-
Notifications
You must be signed in to change notification settings - Fork 2
/
ObservableMapView.java
109 lines (89 loc) · 3.47 KB
/
ObservableMapView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// This work is licensed under the GNU Public License (GPL).
// To view a copy of this license, visit http://www.gnu.org/copyleft/gpl.html
// Written by Abd Allah Diab (mpcabd)
// Email: mpcabd ^at^ gmail ^dot^ com
// Website: http://mpcabd.igeex.biz
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
public class ObservableMapView extends MapView {
public ObservableMapView(Context context, String apiKey) { super(context, apiKey); }
public ObservableMapView(Context context, AttributeSet attrs) { super(context, attrs); }
public ObservableMapView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
private GeoPoint mOldTopLeft;
private GeoPoint mOldCenter;
private GeoPoint mOldBottomRight;
private int mOldZoomLevel = -1;
private MapViewListener mMapViewListener;
public MapViewListener getMapViewListener() { return mMapViewListener; }
public void setMapViewListener(MapViewListener value) { mMapViewListener = value; }
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_UP) {
GeoPoint newCenter = this.getMapCenter();
GeoPoint newTopLeft = this.getProjection().fromPixels(0, 0);
GeoPoint newBottomRight = this.getProjection().fromPixels(this.getWidth(), this.getHeight());
if (this.mMapViewListener != null &&
newTopLeft.getLatitudeE6() == mOldTopLeft.getLatitudeE6() &&
newTopLeft.getLongitudeE6() == mOldTopLeft.getLongitudeE6()) {
mMapViewListener.onClick(this.getProjection().fromPixels((int)ev.getX(), (int)ev.getY()));
}
}
return super.onTouchEvent(ev);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
GeoPoint newCenter = this.getMapCenter();
GeoPoint newTopLeft = this.getProjection().fromPixels(0, 0);
GeoPoint newBottomRight = this.getProjection().fromPixels(this.getWidth(), this.getHeight());
int newZoomLevel = this.getZoomLevel();
if (mOldCenter == null)
mOldCenter = newCenter;
if (mOldTopLeft == null)
mOldTopLeft = newTopLeft;
if (mOldBottomRight == null)
mOldBottomRight = newBottomRight;
if (newTopLeft.getLatitudeE6() != mOldTopLeft.getLatitudeE6() || newTopLeft.getLongitudeE6() != mOldTopLeft.getLongitudeE6()) {
if (this.mMapViewListener != null) {
GeoPoint oldTopLeft, oldCenter, oldBottomRight;
oldTopLeft = mOldTopLeft;
oldCenter = mOldCenter;
oldBottomRight = mOldBottomRight;
mOldBottomRight = newBottomRight;
mOldTopLeft = newTopLeft;
mOldCenter = newCenter;
mMapViewListener.onPan(oldTopLeft,
oldCenter,
oldBottomRight,
newTopLeft,
newCenter,
newBottomRight);
}
}
if (mOldZoomLevel == -1)
mOldZoomLevel = newZoomLevel;
else if (mOldZoomLevel != newZoomLevel && mMapViewListener != null) {
int oldZoomLevel = mOldZoomLevel;
GeoPoint oldTopLeft, oldCenter, oldBottomRight;
oldTopLeft = mOldTopLeft;
oldCenter = mOldCenter;
oldBottomRight = mOldBottomRight;
mOldZoomLevel = newZoomLevel;
mOldBottomRight = newBottomRight;
mOldTopLeft = newTopLeft;
mOldCenter = newCenter;
mMapViewListener.onZoom(oldTopLeft,
oldCenter,
oldBottomRight,
newTopLeft,
newCenter,
newBottomRight,
oldZoomLevel,
newZoomLevel);
}
}
}