java - Get nearest point from ArrayList<LatLng> -
i'm working on app has google map , polyline specify route. want when user opens map automatically put marker on nearest point between user , polyline.
i've set function gets nearest point user polyline. if distance < 600 puts marker in point. want take coordinates(of polyline) < 600 meters user , choose closest one... code
polyline arraylist:
public arraylist<latlng> polylinelist(){ arraylist<latlng> coords = new arraylist<latlng>(); coords.add(new latlng(25.71687391423798,-100.3730825815284)); coords.add(new latlng(25.71682182829175,-100.3733097782636)); coords.add(new latlng(25.71678126641878, -100.374090669652)); coords.add(new latlng(25.71683201122758, -100.3731781269444)); coords.add(new latlng(25.72018196813817, -100.3735084186905)); coords.add(new latlng(25.72031604576068, -100.3736959395207)); coords.add(new latlng(25.71998332400039, -100.3762802484946)); coords.add(new latlng(25.71994816609866, -100.3764646004368)); coords.add(new latlng(25.72208094229626, -100.3773901496331)); coords.add(new latlng(25.72139701948525, -100.3784182403878)); coords.add(new latlng(25.72088085424743, -100.3792655793197)); coords.add(new latlng(25.7211341509678, -100.3796660319109)); coords.add(new latlng(25.72119368137114, -100.3807655073227)); coords.add(new latlng(25.72102639768713, -100.3811907891904)); coords.add(new latlng(25.72079498457438, -100.3810006195533)); coords.add(new latlng(25.720670392499, -100.3799969462212)); coords.add(new latlng(25.72062014771428, -100.3798718252661)); coords.add(new latlng(25.71970136267208, -100.3812225396649)); coords.add(new latlng(25.71935874972525, -100.3814558417726)); coords.add(new latlng(25.71758071083912, -100.3839700351668)); coords.add(new latlng(25.71722347802044, -100.3838886540422)); coords.add(new latlng(25.71531766471047, -100.3815788239292)); coords.add(new latlng(25.71564916096481, -100.3804237189767)); coords.add(new latlng(25.7159267510913, -100.3800793191019)); coords.add(new latlng(25.71627234185268, -100.3795543063391)); coords.add( new latlng(25.71665021282444, -100.3790150094068));
return coords; }
then, user's location , evaluate coordinates every arraylist index using haversine formula, if distace <600 draw marker.
(int = 0; < polylinelist().size(); i++) { double latit = polylinelist.get(i).latitude; double longit = polylinelist.get(i).longitude; latlng newlatlng = new latlng(latit, longit); double distance = distancehaversine(userlatit, latit, userlongit, longit); if (distance < 600) { drawmarker(newlatlng); } }
this working correctly draws lot of markers in coordinates < 600 meters user's location. want draw marker in nearest point of polyline. sort of evaluate coords < 600 user , choose closest. appreciate help.
find minimum distance user available points, latlng
associated min distance , draw marker
using
private int mindistance = 0; private latlng closestlatlng = null; (int = 0; < polylinelist().size(); i++) { double latit = polylinelist.get(i).latitude; double longit = polylinelist.get(i).longitude; latlng newlatlng = new latlng(latit, longit); double distance = distancehaversine(userlatit, latit, userlongit, longit); if (distance < mindistance) { closestlatlng = newlatlng; mindistance = distance; } } if(closestlatlng !=null && mindistance < 600){ drawmarker(closestlatlng); }
Comments
Post a Comment