java - how to make a route/navigation on google-maps from where user location to an itemoverlay that user pick? -
i'm confuse, how create action create route/navigation user location pin overlay(google places) user pick/tap pin overlay. this's map activity show map.
public class placesmapactivity extends mapactivity { // nearest places placeslist nearplaces; // map view mapview mapview; // map overlay items list<overlay> mapoverlays; additemizedoverlay itemizedoverlay; geopoint geopoint; // map controllers mapcontroller mc; double latitude; double longitude; overlayitem overlayitem; private context context; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.map_places); // getting intent info intent = getintent(); // users current geo location string user_latitude = i.getstringextra("user_latitude"); string user_longitude = i.getstringextra("user_longitude"); // nearplaces list nearplaces = (placeslist) i.getserializableextra("near_places"); mapview = (mapview) findviewbyid(r.id.mapview); mapview.setbuiltinzoomcontrols(true); mapoverlays = mapview.getoverlays(); // geopoint place on map geopoint = new geopoint((int) (double.parsedouble(user_latitude) * 1e6), (int) (double.parsedouble(user_longitude) * 1e6)); // drawable marker icon drawable drawable_user = this.getresources() .getdrawable(r.drawable.mark_red); itemizedoverlay = new additemizedoverlay(drawable_user, this); // map overlay item overlayitem = new overlayitem(geopoint, "your location", "that you!"); itemizedoverlay.addoverlay(overlayitem); mapoverlays.add(itemizedoverlay); itemizedoverlay.populatenow(); // drawable marker icon drawable drawable = this.getresources() .getdrawable(r.drawable.mark_blue); itemizedoverlay = new additemizedoverlay(drawable, this); mc = mapview.getcontroller(); // these values used map boundary area // area can see markers on screen int minlat = integer.max_value; int minlong = integer.max_value; int maxlat = integer.min_value; int maxlong = integer.min_value; // check null in case null if (nearplaces.results != null) { // loop through places (place place : nearplaces.results) { latitude = place.geometry.location.lat; // latitude longitude = place.geometry.location.lng; // longitude // geopoint place on map geopoint = new geopoint((int) (latitude * 1e6), (int) (longitude * 1e6)); // map overlay item overlayitem = new overlayitem(geopoint, place.name, place.vicinity); itemizedoverlay.addoverlay(overlayitem); // calculating map boundary area minlat = (int) math.min( geopoint.getlatitudee6(), minlat ); minlong = (int) math.min( geopoint.getlongitudee6(), minlong); maxlat = (int) math.max( geopoint.getlatitudee6(), maxlat ); maxlong = (int) math.max( geopoint.getlongitudee6(), maxlong ); } mapoverlays.add(itemizedoverlay); // showing overlay items itemizedoverlay.populatenow(); } // adjusting zoom level can see markers on map mapview.getcontroller().zoomtospan(math.abs( minlat - maxlat ), math.abs( minlong - maxlong )); // showing center of map mc.animateto(new geopoint((maxlat + minlat)/2, (maxlong + minlong)/2 )); mapview.postinvalidate(); } @override protected boolean isroutedisplayed() { homecoming false; }
}
and this's itemoverlay show pin on map.
public class additemizedoverlay extends itemizedoverlay<overlayitem> { private arraylist<overlayitem> mapoverlays = new arraylist<overlayitem>(); private context context; public additemizedoverlay(drawable defaultmarker) { super(boundcenterbottom(defaultmarker)); } public additemizedoverlay(drawable defaultmarker, context context) { this(defaultmarker); this.context = context; } @override public boolean ontouchevent(motionevent event, mapview mapview) { if (event.getaction() == 1) { geopoint geopoint = mapview.getprojection().frompixels( (int) event.getx(), (int) event.gety()); // latitude double lat = geopoint.getlatitudee6() / 1e6; // longitude double lon = geopoint.getlongitudee6() / 1e6; //toast.maketext(context, "lat: " + lat + ", lon: "+lon, toast.length_short).show(); } homecoming false; } @override protected overlayitem createitem(int i) { homecoming mapoverlays.get(i); } @override public int size() { homecoming mapoverlays.size(); } @override protected boolean ontap(int index) { overlayitem item = mapoverlays.get(index); alertdialog.builder dialog = new alertdialog.builder(this.context); dialog.settitle(item.gettitle()); dialog.setmessage(item.getsnippet()); dialog.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { } }); dialog.show(); homecoming true; } public void addoverlay(overlayitem overlay) { mapoverlays.add(overlay); } public void populatenow(){ this.populate(); } }
please give me opinion.
use "mapcontroller" class's function animateto(your_desired_point). utilize in additemizedoverlay class in ontap().
java android google-maps routes overlay
No comments:
Post a Comment