Wednesday, 15 September 2010

Highlight a specified route on Google Maps v2 Android -



Highlight a specified route on Google Maps v2 Android -

right, i'm using google directions api in app retrieve route between 2 locations.

when send request route directions, retrieve number of details in json regarding route including names of every road along route, corresponding start , end lat-long co-ordinates, , polyline value.

for example: if send request http://maps.googleapis.com/maps/api/directions/json?origin=redfern+ave,+dublin&destination=limetree+ave,+dublin&sensor=false between 2 roads, next json response (output 1 road along route).

{ "distance" : { "text" : "0.2 km", "value" : 203 }, "duration" : { "text" : "1 min", "value" : 18 }, "end_location" : { "lat" : 53.435250, "lng" : -6.132140000000001 }, "html_instructions" : "head \u003cb\u003eeast\u003c/b\u003e on \u003cb\u003eredfern ave.\u003c/b\u003e toward \u003cb\u003emartello court\u003c/b\u003e", **"polyline" : { "points" : "woceivgmd@o}dokdqqf"** },

so far application parses info , lists roads , directions in list view this:

what want highlight whole route b on map, i've found nil useful online on how on new google maps api v2. see polyline's used instead of overlays draw lines on google maps v2, can tell, draw straight lines useless me. there anyway of highlighting route using info have @ disposal (road names, start & end lat-long co-ordinates, polyline points? help appreciated.

also, see there 'polyline' value in response useful can't work out how parse or utilize bit of information. know how can create sense of value plot polyline?

**"polyline" : { "points" : "woceivgmd@o}dokdqqf"**

edit: solution code provided in reply below.

i managed working after lot of trial , error! highlights specified route b on map (as seen in screenshot below). have thrown in code may need in future.

public class polymap extends activity { progressdialog pdialog; googlemap map; list<latlng> polyz; jsonarray array; static final latlng dublin = new latlng(53.344103999999990000, -6.267493699999932000); @suppresslint("newapi") protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.map_layout); map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)) .getmap(); map.movecamera(cameraupdatefactory.newlatlngzoom(dublin, 15)); map.animatecamera(cameraupdatefactory.zoomto(10), 2000, null); new getdirection().execute(); } class getdirection extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(polymap.this); pdialog.setmessage("loading route. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } protected string doinbackground(string... args) { intent = getintent(); string startlocation = i.getstringextra("startloc"); string endlocation = i.getstringextra("endloc"); startlocation = startlocation.replace(" ", "+"); endlocation = endlocation.replace(" ", "+");; string stringurl = "http://maps.googleapis.com/maps/api/directions/json?origin=" + startlocation + ",+dublin&destination=" + endlocation + ",+dublin&sensor=false"; stringbuilder response = new stringbuilder(); seek { url url = new url(stringurl); httpurlconnection httpconn = (httpurlconnection) url .openconnection(); if (httpconn.getresponsecode() == httpurlconnection.http_ok) { bufferedreader input = new bufferedreader( new inputstreamreader(httpconn.getinputstream()), 8192); string strline = null; while ((strline = input.readline()) != null) { response.append(strline); } input.close(); } string jsonoutput = response.tostring(); jsonobject jsonobject = new jsonobject(jsonoutput); // routesarray contains routes jsonarray routesarray = jsonobject.getjsonarray("routes"); // grab first route jsonobject route = routesarray.getjsonobject(0); jsonobject poly = route.getjsonobject("overview_polyline"); string polyline = poly.getstring("points"); polyz = decodepoly(polyline); } grab (exception e) { } homecoming null; } protected void onpostexecute(string file_url) { (int = 0; < polyz.size() - 1; i++) { latlng src = polyz.get(i); latlng dest = polyz.get(i + 1); polyline line = map.addpolyline(new polylineoptions() .add(new latlng(src.latitude, src.longitude), new latlng(dest.latitude, dest.longitude)) .width(2).color(color.red).geodesic(true)); } pdialog.dismiss(); } } /* method decode polyline points */ private list<latlng> decodepoly(string encoded) { list<latlng> poly = new arraylist<latlng>(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; { b = encoded.charat(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; { b = encoded.charat(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; latlng p = new latlng((((double) lat / 1e5)), (((double) lng / 1e5))); poly.add(p); } homecoming poly; } }

android highlight routes google-maps-android-api-2 directions

No comments:

Post a Comment