Android Google Map routing -


hi first time using map in android after changing in google map api.

now wants draw route on map between 2 addresses endered me api v2. don't know how this. tried lot this. please me. thanks.

my code is:

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     requestwindowfeature(window.feature_no_title);     setcontentview(r.layout.start_trip_view);     try {         arraylist<string> location = new arraylist<string>();         intent ii = getintent();          location2 = (ii.getstringextra("place"));         string location3 = (ii.getstringextra("start"));         gps = new gpstracker(getapplicationcontext());         latitude = gps.getlatitude();         longitude = gps.getlongitude();         balvinder = new latlng(latitude, longitude);         markerpoints = new arraylist<latlng>();          map = ((mapfragment) getfragmentmanager()                 .findfragmentbyid(r.id.map)).getmap();         map.setmylocationenabled(true);          //          if (location == null || location.equals("")) {             toast.maketext(getbasecontext(), "no place entered",                     toast.length_short).show();             return;         }          string url = "https://maps.googleapis.com/maps/api/geocode/json?";         // for(int i=0; i<location.size();i++)         // {         try {             // encoding special characters space in user input             // place              location2 = urlencoder.encode(location2, "utf-8"); //              location3 = urlencoder.encode(location3, "utf-8");             string saddress = "address=" + location2;             // string dsaddress = "address=" + location3;              string sensor = "sensor=false";              // url , geocoding data fetched              url = url + saddress + "&" + sensor;              downloadtask downloadtask = new downloadtask();              // start downloading geocoding places             downloadtask.execute(url);             }   private string downloadurl(string... strurl) throws ioexception {     string data = "";     inputstream istream = null;     httpurlconnection urlconnection = null;     try {         for(int i=0;i<strurl.length;i++)         {         url url = new url(strurl[i]);           // creating http connection communicate url         urlconnection = (httpurlconnection) url.openconnection();          // connecting url         urlconnection.connect();          // reading data url         istream = urlconnection.getinputstream();          bufferedreader br = new bufferedreader(new inputstreamreader(                 istream));          stringbuffer sb = new stringbuffer();          string line = "";         while ((line = br.readline()) != null) {             sb.append(line);         }          data = sb.tostring();          br.close();         }     } catch (exception e) {         log.d("exception while downloading url", e.tostring());     } {         istream.close();         urlconnection.disconnect();     }      return data;  }  // fetches data url passed private class downloadtask extends asynctask<string, integer, string> {      string data = null;      // invoked execute() method of object     @override     protected string doinbackground(string... url) {         try {             for(int i=0;i<url.length;i++)             {             data = downloadurl(url[i]);         }         } catch (exception e) {             log.d("background task", e.tostring());         }         return data;     }      // executed after complete execution of doinbackground() method     @override     protected void onpostexecute(string result) {          // instantiating parsertask parses json data         // geocoding webservice         // in non-ui thread            parsertask parsertask = new parsertask();         parsertask.execute(result);         }         system.out.println("result"+result);      }  }  /** class parse google places in json format */ class parsertask extends         asynctask<string, integer, list<hashmap<string, string>>> {      jsonobject jobject;      // invoked execute() method of object     @override     protected list<hashmap<string, string>> doinbackground(             string... jsondata) {          list<hashmap<string, string>> places = null;         geocodejsonparser parser = new geocodejsonparser();          try {             jobject = new jsonobject(jsondata[0]);              /** getting parsed data arraylist */             places = parser.parse(jobject);          } catch (exception e) {             log.d("exception", e.tostring());         }         return places;     }      // executed after complete execution of doinbackground() method     @override     protected void onpostexecute(list<hashmap<string, string>> list) {         polylineoptions lineoptions = new polylineoptions();         arraylist<latlng>points=new arraylist<latlng>();         // clears existing markers         map.clear();          (int = 0; < list.size(); i++) {              // creating marker             markeroptions markeroptions = new markeroptions();              // getting place places list             hashmap<string, string> hmplace = list.get(i);              // getting latitude of place              lat = double.parsedouble(hmplace.get("lat"));              // getting longitude of place              lng = double.parsedouble(hmplace.get("lng"));              // getting name             string name = hmplace.get("formatted_address");              latlng = new latlng(lat, lng);              // setting position marker             markeroptions.position(latlng);             // getdirectionsurl(balvinder, latlng);             // markeroptions.position(balvinder);                      .show();     map.addmarker(new markeroptions().title("my location").snippet(     gps.convertpointtolocation(latitude, longitude,                             starttripview.this))             .position(balvinder)             .icon(bitmapdescriptorfactory                     .fromresource(r.drawable.logo_sono)));             markerpoints.add(balvinder);             markerpoints.add(latlng);             //makeurl(latitude, longitude, lat, lng);     system.out.println("result"+makeurl(latitude, longitude, lat, lng));             // polyline line = map.addpolyline(new polylineoptions()             // .add(balvinder, latlng)             // .width(5)             // .color(color.red).geodesic(true));                 markeroptions.title(name);             //             map.addmarker(markeroptions);      if (i == 0){           map.animatecamera(cameraupdatefactory.newlatlng(latlng));             }          }   }  } 

documentation: https://developers.google.com/maps/documentation/android/shapes?hl=fr#polylines

to draw route between 2 locations, should use polyline:

// instantiates new polyline object , adds points define rectangle polylineoptions rectoptions = new polylineoptions()         .add(new latlng(37.35, -122.0))         .add(new latlng(37.45, -122.0))  // north of previous point, @ same longitude         .add(new latlng(37.45, -122.2))  // same latitude, , 30km west         .add(new latlng(37.35, -122.2))  // same longitude, , 16km south         .add(new latlng(37.35, -122.0)); // closes polyline.  // mutable polyline polyline polyline = mymap.addpolyline(rectoptions); 

in case won't need last point close polyline.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -