Map Showing Blue Screen ,when drawing Path Between Two Places in Android -


in android google map , want draw path between 2 places.

when iam using hardcoded latitude , longitude showing perfectly.

problem-1

but latitude , longitude getting other intent showing blue screen in map

then closing page again opening showing path between 2 points.

problem-2

then iam changing latitude , longitude of both locations showing previous location path.

here latitute , longitude values iam getting previous intent.

how resolve it. have added code

mainactivity .java

    public class mainactivity extends fragmentactivity implements oninfowindowclicklistener{           public double latitude1_current_double,longitutde1_current_double,latitude1_center_double,longitude1_center_double;           private int zoomlevel = 11;           latlng origin1 = new latlng(latitude1_current_double, longitutde1_current_double);             latlng dest1 = new latlng(latitude1_service_double,longitude1_service_double);              double lat1=latitude1_current_double;             double lang1=longitutde1_current_double;             double lat2=latitude1_center_double;             double lang2=longitude1_center_double;               googlemap map;             arraylist<latlng> markerpoints;               /*private int zoomlevel1 = 14;              private int zoomlevel2 = 11;*/          @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_main);              bundle extras = getintent().getextras();               string latitude1_current_string=extras.getstring("string_i_need_latitude_current");             string longitutde1_current_string =extras.getstring("string_i_need_longitude_current");             string latitude1_servicecenter_string =extras.getstring("string_i_need_latitude_servicecenter");             string longitude1_servicecenter_string =extras.getstring("string_i_need_longitude_servicecenter");                  latitude1_current_double = double.parsedouble(latitude1_current_string);                 longitutde1_current_double = double.parsedouble(longitutde1_current_string);                 latitude1_servicecenter_double = double.parsedouble(latitude1_servicecenter_string);                 longitude1_servicecenter_double = double.parsedouble(longitude1_servicecenter_string);               toast.maketext(getapplicationcontext(), "get direction"+latitude1_current_double+","+longitutde1_current_double+                     latitude1_center_double+","+longitude1_center_double,toast.length_short).show();                // initializing              markerpoints = new arraylist<latlng>();              // getting reference supportmapfragment of activity_main             supportmapfragment fm = (supportmapfragment)getsupportfragmentmanager().findfragmentbyid(r.id.map);              // getting reference button         //  button btndraw = (button)findviewbyid(r.id.btn_draw);                     // getting map supportmapfragment             map = fm.getmap();              // enable mylocation button in map             map.setmylocationenabled(true);                      map.addmarker(new markeroptions().position(origin1)                      .title("user current location")                      .snippet("latlng"+"("+lat1+","+lang1+")")                      .icon(bitmapdescriptorfactory                         .defaultmarker(bitmapdescriptorfactory.hue_green)));               map.addmarker(new markeroptions().position(dest1)                      .title("service center destination location")                      .snippet("latlng"+"("+lat2+","+lang2+")")                      .icon(bitmapdescriptorfactory                       .defaultmarker(bitmapdescriptorfactory.hue_red)));               map.movecamera(cameraupdatefactory.newlatlngzoom(origin1, zoomlevel));             map.movecamera(cameraupdatefactory.newlatlngzoom(dest1, zoomlevel));              markerpoints.add(origin1);             markerpoints.add(dest1);               // getting url google directions api             string url = getdirectionsurl(origin1, dest1);                            downloadtask downloadtask = new downloadtask();              // start downloading json data google directions api             downloadtask.execute(url);                 // map cleared on long click             map.setonmaplongclicklistener(new onmaplongclicklistener() {                  @override                 public void onmaplongclick(latlng point) {                     // removes points google map                     map.clear();                      // removes points in arraylist                     markerpoints.clear();                  }             });             }          private string getdirectionsurl(latlng origin,latlng dest){              // origin of route             string str_origin = "origin="+origin.latitude+","+origin.longitude;              // destination of route             string str_dest = "destination="+dest.latitude+","+dest.longitude;                    // sensor enabled             string sensor = "sensor=false";                       // waypoints             string waypoints = "";              for(int i=2;i<markerpoints.size();i++)             {                 latlng point  = (latlng) markerpoints.get(i);                 if(i==2)                 waypoints = "waypoints=";                 waypoints += point.latitude + "," + point.longitude + "|";             }               // building parameters web service             string parameters = str_origin+"&"+str_dest+"&"+sensor+"&"+waypoints;              // output format             string output = "json";              // building url web service             string url = "https://maps.googleapis.com/maps/api/directions/"+output+"?"+parameters;               return url;         }          /** method download json data url */         private string downloadurl(string strurl) throws ioexception{             string data = "";             inputstream istream = null;             httpurlconnection urlconnection = null;             try{                     url url = new url(strurl);                      // 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());             }finally{                     istream.close();                     urlconnection.disconnect();             }             return data;          }            // fetches data url passed         private class downloadtask extends asynctask<string, void, string>{                       // downloading data in non-ui thread             @override             protected string doinbackground(string... url) {                  // storing data web service                 string data = "";                  try{                     // fetching data web service                     data = downloadurl(url[0]);                 }catch(exception e){                     log.d("background task",e.tostring());                 }                 return data;                     }              // executes in ui thread, after execution of             // doinbackground()             @override             protected void onpostexecute(string result)              {                            super.onpostexecute(result);                              try                 {                 parsertask parsertask = new parsertask();                  // invokes thread parsing json data                 parsertask.execute(result);              }              catch (exception e)             {                 e.printstacktrace();                 log.i("exceptoin in downloadtask onpostexecute", "errroor");             }              }                }          /** class parse google places in json format */         private class parsertask extends asynctask<string, integer, list<list<hashmap<string,string>>> >{              // parsing data in non-ui thread                     @override             protected list<list<hashmap<string, string>>> doinbackground(string... jsondata) {                  jsonobject jobject;                  list<list<hashmap<string, string>>> routes = null;                                       try{                     jobject = new jsonobject(jsondata[0]);                     directionsjsonparser parser = new directionsjsonparser();                      // starts parsing data                     routes = parser.parse(jobject);                     }catch(exception e){                     e.printstacktrace();                 }                 return routes;             }              // executes in ui thread, after parsing process             @override             protected void onpostexecute(list<list<hashmap<string, string>>> result)             {                  try                 {                  arraylist<latlng> points = null;                 polylineoptions lineoptions = null;                  // traversing through routes                 for(int i=0;i<result.size();i++){                     points = new arraylist<latlng>();                     lineoptions = new polylineoptions();                      // fetching i-th route                     list<hashmap<string, string>> path = result.get(i);                      // fetching points in i-th route                     for(int j=0;j<path.size();j++){                         hashmap<string,string> point = path.get(j);                                           double lat = double.parsedouble(point.get("lat"));                         double lng = double.parsedouble(point.get("lng"));                         latlng position = new latlng(lat, lng);                           points.add(position);                                            }                      // adding points in route lineoptions                     lineoptions.addall(points);                     lineoptions.width(5);                     lineoptions.color(color.blue);                               }                  // drawing polyline in google map i-th route                 map.addpolyline(lineoptions);                 }              catch (exception e)             {                 e.printstacktrace();                 log.i("exceptoin in  parsing process onpostexecute", "errroor");             }              }                    }              @override         public boolean oncreateoptionsmenu(menu menu) {             // inflate menu; adds items action bar if present.             getmenuinflater().inflate(r.menu.main, menu);             return true;         }          @override         public void oninfowindowclick(marker marker)         {              intent intent = new intent(this, newactivity.class);             intent.putextra("snippet", marker.getsnippet());             intent.putextra("title", marker.gettitle());             intent.putextra("position", marker.getposition());             startactivity(intent);          }        } 

directionsjsonparser.java

import com.google.android.gms.maps.model.latlng;  public class directionsjsonparser {      /** receives jsonobject , returns list of lists containing latitude , longitude */     public list<list<hashmap<string,string>>> parse(jsonobject jobject){          list<list<hashmap<string, string>>> routes = new arraylist<list<hashmap<string,string>>>();         jsonarray jroutes = null;         jsonarray jlegs = null;         jsonarray jsteps = null;              try {              jroutes = jobject.getjsonarray("routes");              /** traversing routes */             for(int i=0;i<jroutes.length();i++){                         jlegs = ( (jsonobject)jroutes.get(i)).getjsonarray("legs");                 list path = new arraylist<hashmap<string, string>>();                  /** traversing legs */                 for(int j=0;j<jlegs.length();j++){                     jsteps = ( (jsonobject)jlegs.get(j)).getjsonarray("steps");                      /** traversing steps */                     for(int k=0;k<jsteps.length();k++){                         string polyline = "";                         polyline = (string)((jsonobject)((jsonobject)jsteps.get(k)).get("polyline")).get("points");                         list<latlng> list = decodepoly(polyline);                          /** traversing points */                         for(int l=0;l<list.size();l++){                             hashmap<string, string> hm = new hashmap<string, string>();                             hm.put("lat", double.tostring(((latlng)list.get(l)).latitude) );                             hm.put("lng", double.tostring(((latlng)list.get(l)).longitude) );                             path.add(hm);                                                }                                                    }                     routes.add(path);                 }             }          } catch (jsonexception e) {                      e.printstacktrace();         }catch (exception e){                    }           return routes;     }          /**      * method decode polyline points       * courtesy : jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java       * */     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);         }          return poly;     } } 

figured out problem:

    public double latitude1_current_double,longitutde1_current_double,latitude1_center_double,longitude1_center_double;   private int zoomlevel = 11;   latlng origin1 = new latlng(latitude1_current_double, longitutde1_current_double);     latlng dest1 = new latlng(latitude1_service_double,longitude1_service_double);      double lat1=latitude1_current_double;     double lang1=longitutde1_current_double;     double lat2=latitude1_center_double;     double lang2=longitude1_center_double; 

the variables origin1 , dest1 not change if change lat1 , lang1, map.addmarker(new markeroptions().position(origin1) have origin1 =(0,0) default value. need make sure variables origin1 , dest1 modified inside oncreate method markers not placed in middle of ocean. hope clarifies. i've tested code , put origin1 , dest1 correct lat/lng info in oncreate method, i've got desired result.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -