-
Notifications
You must be signed in to change notification settings - Fork 982
/
CirclePlottingOverlay.java
66 lines (54 loc) · 2.34 KB
/
CirclePlottingOverlay.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
package org.osmdroid.samplefragments.drawing;
import android.util.Log;
import android.view.MotionEvent;
import org.osmdroid.api.IMapView;
import org.osmdroid.config.Configuration;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.overlay.Polygon;
import java.util.List;
/**
* created on 12/27/2017.
*
* @author Alex O'Ree
*/
public class CirclePlottingOverlay extends Overlay {
float distanceKm;
public CirclePlottingOverlay(float distanceKm) {
super();
this.distanceKm = distanceKm;
}
@Override
public boolean onLongPress(final MotionEvent e, final MapView mapView) {
if (Configuration.getInstance().isDebugMapView()) {
Log.d(IMapView.LOGTAG, "CirclePlottingOverlay onLongPress");
}
GeoPoint pt = (GeoPoint) mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY(), null);
/*
* <b>Note</b></b: when plotting a point off the map, the conversion from
* screen coordinates to map coordinates will return values that are invalid from a latitude,longitude
* perspective. Sometimes this is a wanted behavior and sometimes it isn't. We are leaving it up to you,
* the developer using osmdroid to decide on what is right for your application. See
* <a href="https://github.com/osmdroid/osmdroid/pull/722">https://github.com/osmdroid/osmdroid/pull/722</a>
* for more information and the discussion associated with this.
*/
//just in case the point is off the map, let's fix the coordinates
if (pt.getLongitude() < -180)
pt.setLongitude(pt.getLongitude() + 360);
if (pt.getLongitude() > 180)
pt.setLongitude(pt.getLongitude() - 360);
//latitude is a bit harder. see https://en.wikipedia.org/wiki/Mercator_projection
if (pt.getLatitude() > 85.05112877980659)
pt.setLatitude(85.05112877980659);
if (pt.getLatitude() < -85.05112877980659)
pt.setLatitude(-85.05112877980659);
List<GeoPoint> circle = Polygon.pointsAsCircle(pt, distanceKm);
Polygon p = new Polygon(mapView);
p.setPoints(circle);
p.setTitle("A circle");
mapView.getOverlayManager().add(p);
mapView.invalidate();
return true;
}
}