java - How to tell if a Marker is in a Polygon googleMap v2 -
i've got google map polygons
, i've got work except last part detect if marker
inside polygon
.the current situation when touch polygon
add marker
"which correct" if touch polygon
in different spot remove marker
, add new one. want happen if marker
within points don't add new one. code below appreciate.
public void onmapclick(latlng point) { (int j = 0; j < arrpolygons.size(); j++) { if (ispointinpolygon(point, arrpolygons.get(j).getpoints())) { if (marker != null) { marker.remove(); marker = null; log.v("marker", "removing marker"); }else{ marker = googlemap.addmarker(new markeroptions() .position(point) .title("test") .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_red))); log.v("marker", "adding marker"); } } }
any appreciated.
the easiest way using google maps android api utility library, contains polyutil class.
first import library adding current version build.gradle, 0.3.4
example:
dependencies { compile filetree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.google.android.gms:play-services-maps:7.3.0' compile 'com.google.maps.android:android-maps-utils:0.3.4' }
for simple example we'll assume map activity class definition , member variables this, , 1 polygon defined in polygonlist
:
import com.google.maps.android.polyutil; //other imports..... public class mapsactivity extends appcompatactivity implements onmapreadycallback { private googlemap mmap; private marker marker; list<latlng> polygonlist = new arraylist<latlng>(); //.............
you set googlemap.onmapclicklistener
in order add 1 marker inside polygon.:
mmap.setonmapclicklistener(new googlemap.onmapclicklistener() { @override public void onmapclick(latlng point) { if (polyutil.containslocation(point, polygonlist, false)) { if (marker == null) { //only add marker if there not 1 inside polygon marker = mmap.addmarker(new markeroptions() .position(point) .title("test") .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_red))); log.v("marker", "adding marker"); } } } });
multiple polygons, multiple markers solution:
in order make work multiple ploygons, use pojo store polygon/marker pairs:
public class polymarkerobject{ polygon polygon; marker marker; }
then define new member variable polymarkerlist
:
public class mapsactivity extends appcompatactivity implements onmapreadycallback { private googlemap mmap; list<polymarkerobject> polymarkerlist = new arraylist<>(); //.............
add each polygon list when drawing it:
list<latlng> newpolygon = new arraylist<>(); //set points in polygon....... polygon p = mmap.addpolygon(new polygonoptions() .addall(newpolygon) .strokecolor(color.red) .fillcolor(color.blue)); polymarkerobject newpolymarkerobj = new polymarkerobject(); newpolymarkerobj.polygon = p; polymarkerlist.add(newpolymarkerobj);
then cycle through list on each map click see whether current ploygon has marker. if not have marker already, remove marker placed in different polygon, , create 1 in current polygon:
mmap.setonmapclicklistener(new googlemap.onmapclicklistener() { @override public void onmapclick(latlng point) { (polymarkerobject pmobj : polymarkerlist) { //only add marker if there not 1 inside polygon if (polyutil.containslocation(point, pmobj.polygon.getpoints(), false)) { if (pmobj.marker == null) { //add marker current polygon marker newmarker = mmap.addmarker(new markeroptions() .position(point) .title("test") .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_red))); pmobj.marker = newmarker; log.v("marker", "adding marker"); break; } } } } });
multiple polygons, 1 marker solution:
in case, need 1 marker reference, , list of polygons:
public class mapsactivity extends appcompatactivity implements onmapreadycallback { private googlemap mmap; marker marker; list<polygon> polylist = new arraylist<>(); //................
add polygon list when it's added map:
list<latlng> newpolygon = new arraylist<>(); //set points in polygon....... polygon p = mmap.addpolygon(new polygonoptions() .addall(newpolygon) .strokecolor(color.red) .fillcolor(color.blue)); polylist.add(p);
then, in map click listener, have 2 cases, 1 if marker reference null (no marker added yet), , 1 if marker in different polygon. if marker in current polygon already, not moved.
mmap.setonmapclicklistener(new googlemap.onmapclicklistener() { @override public void onmapclick(latlng point) { (polygon pobj : polylist) { //find polygon user tapped inside of if (polyutil.containslocation(point, pobj.getpoints(), false)) { //first case, no marker if (marker == null) { //add marker current polygon marker = mmap.addmarker(new markeroptions() .position(point) .title("test") .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_red))); log.v("marker", "adding first marker"); break; } else if (!polyutil.containslocation(marker.getposition(), pobj.getpoints(), false)) { //marker exists in different polygon //remove marker previous polygon marker.remove(); //add marker current polygon marker = mmap.addmarker(new markeroptions() .position(point) .title("test") .icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_red))); log.v("marker", "moving marker new polygon"); break; } } } } });
Comments
Post a Comment