Getting Direction Between Two Points In Google Maps, Crossing Specific Points
I'm working on a project that the main part is to visually demonstrates directions between two points on Google Maps, but I want this direction do pass from specific point like A a
Solution 1:
If you're obtaining the route by explicitly parsing an URL to Google try the following code, it will allow you to set the PolyLine
parameters:
// URL constructorpublic String makeURL(double sourcelat, double sourcelog, double destlat, double destlog ){
StringBuilderurlString=newStringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json");
urlString.append("?origin=");// from
urlString.append(Double.toString(sourcelat));
urlString.append(",");
urlString.append(Double.toString( sourcelog));
urlString.append("&destination=");// to
urlString.append(Double.toString( destlat));
urlString.append(",");
urlString.append(Double.toString( destlog));
urlString.append("&sensor=false&mode=walking&alternatives=true");
return urlString.toString();
}
// draws path from result StringpublicvoiddrawPath(String result) {
try {
//Tranform the string into a json objectfinalJSONObjectjson=newJSONObject(result);
JSONArrayrouteArray= json.getJSONArray("routes");
JSONObjectroutes= routeArray.getJSONObject(0);
JSONObjectoverviewPolylines= routes.getJSONObject("overview_polyline");
StringencodedString= overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
for(intz=0; z<list.size()-1;z++){
LatLng src= list.get(z);
LatLng dest= list.get(z+1);
map.addPolyline(newPolylineOptions()
.add(newLatLng(src.latitude, src.longitude), newLatLng(dest.latitude, dest.longitude))
.width(8)
.color(Color.BLUE).geodesic(true));
}
}
catch (JSONException e) {
}
}
// obtain route using AsyncTaskprivateclassconnectAsyncTaskextendsAsyncTask<Void, Void, String>{
private ProgressDialog progressDialog;
String url;
connectAsyncTask(String urlPass){
url = urlPass;
}
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
progressDialog = newProgressDialog(DisplayOnMapActivity.this);
progressDialog.setMessage(getResources().getString(R.string.waitCoord));
progressDialog.setIndeterminate(true);
progressDialog.show();
}
@Overrideprotected String doInBackground(Void... params) {
JSONParserjParser=newJSONParser();
Stringjson= jParser.getJSONFromUrl(url);
return json;
}
@OverrideprotectedvoidonPostExecute(String result) {
super.onPostExecute(result);
progressDialog.hide();
if(result!=null){
drawPath(result);
}
}
}
publicvoiddrawPath(LatLng src, LatLng dest) {
Stringurl= makeURL(src.latitude,src.longitude,dest.latitude,dest.longitude);
newconnectAsyncTask(url).execute();
}
// decodes encoded String into PolyLine objectprivate List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = newArrayList<LatLng>();
intindex=0, len = encoded.length();
intlat=0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
intdlat= ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
intdlng= ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLngp=newLatLng( (((double) lat / 1E5)),
(((double) lng / 1E5) ));
poly.add(p);
}
return poly;
}
You will also need the JSONParser class
:
publicclassJSONParser {
staticInputStreamis=null;
staticJSONObjectjobj=null;
staticStringjson="";
publicJSONParser(){
}
public JSONObject makeHttpRequest(String url){
DefaultHttpClienthttpclient=newDefaultHttpClient();
HttpPosthttppost=newHttpPost(url);
try {
HttpResponsehttpresponse= httpclient.execute(httppost);
if(httpresponse!=null){
HttpEntityhttpentity= httpresponse.getEntity();
is = httpentity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(is!=null){
BufferedReaderreader=newBufferedReader(newInputStreamReader(is,"iso-8859-1"),8);
StringBuildersb=newStringBuilder();
Stringline=null;
try {
while((line = reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
json = sb.toString();
try {
jobj = newJSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return jobj;
}
public String getJSONFromUrl(String url) {
// Making HTTP requesttry {
// defaultHttpClientDefaultHttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPost=newHttpPost(url);
HttpResponsehttpResponse= httpClient.execute(httpPost);
HttpEntityhttpEntity= httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReaderreader=newBufferedReader(newInputStreamReader(
is, "iso-8859-1"), 8);
StringBuildersb=newStringBuilder();
Stringline=null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
json = sb.toString();
is.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return json;
}
}
Post a Comment for "Getting Direction Between Two Points In Google Maps, Crossing Specific Points"