Voice Guidance for Google Maps Route in Android - android

My application calls Google Maps via an Intent to show best route between two points using this code:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?dirflg=d&saddr=" + String.valueOf(currentPosition.getPosition().latitude) + "," + String.valueOf(currentPosition.getPosition().longitude) + "&daddr=" + String.valueOf(currentDest.getPosition().latitude) + "," + String.valueOf(currentDest.getPosition().longitude)));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
I was wondering, is it possible to activate voice guidance for the route via a parameter in the URL?

Use this code for voice guidance and navigation
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("google.navigation:q="
+Destination));
startActivity(i);

Related

How can I enforce returning to my app when starting google navigation with an intent

I call Google Maps with an Intent like this:
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + lat + "," + lon + "&mode=d"));
mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Now I want to return to my app automatically when arriving at the destination. How can I achieve that?
Bonus question: Can I retrieve some information from Maps about the route like duration?

How to show distance between two points on map

I have latitude and longutude of two points. I want to show the route between them by opening google maps from my app on button click. How to do so?
Use below code for getting show route in google map...
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("
http://maps.google.com/maps?" +"saddr=" + sourcelatLng + "&daddr=" + destlatlng;
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
If you want to open maps app with a route from your source to destination, you just need to start an activity with following intent.
Uri routeUri = Uri.parse("http://maps.google.com/maps?saddr=your-lat-1,your-lng-1&daddr=your-lat-2,your-lng-2");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, routeUri);
startActivity(intent);
Replace your-lat/lng-1/2 with your latitude and longitude.

Google Maps 7 started with geo intent is dropping a pin at the closest address to a coordinate instead of the exact location

In my app users can save the latitude and longitude of a particular location. I want them to able to launch other apps on their phone via a geo intent such as Google Maps so that they can easily get exact directions back to it. Here is the code I am using to generate the geo intent:
public static Intent getFindIntent(Context context) {
Intent intent = new Intent();
SharedPreferences prefs = Tools.getPrefs(context);
String latitude = prefs.getString(Const.LAT_KEY, "");
String longitude = prefs.getString(Const.LONG_KEY, "");
intent.setAction(Intent.ACTION_VIEW);
String uri = String.format(Locale.US, "geo:%s,%s?z=%d&q=%s,%s", latitude, longitude,
Const.ZOOM_LEVEL, latitude, longitude);
intent.setData(Uri.parse(uri));
return intent;
}
Obviously most people will use Google Maps since it's on everyone's phone. When I first released my application Google Maps would start and drop a pin at the exact coordinates specified in the q parameter of the geo uri that is set as data for the intent. With the release of Google Maps 7, maps seems to drop a pin at the closest address to the provided coordinates. This poses a problem to me since users should be able to navigate back to the saved position exactly, and not an address near it. The only place I have found this documented is here. The docs are from the official Android Developer site but they are very sparse. Are there alternatives to the geo intent to provide this functionality or is there something wrong with my code? I think Google Maps is breaking it's API contract since I am providing coordinates and not an address.
To drop the pin use this code:
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:" + AppointmentDetailLayout.docLatitude
+ "," + AppointmentDetailLayout.docLongitude
+ "?q=" + AppointmentDetailLayout.docLatitude
+ "," + AppointmentDetailLayout.docLongitude
+ "(" + label + ")"));
intent.setComponent(new ComponentName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity"));
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
try {
context.startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.google.android.apps.maps")));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")));
}
e.printStackTrace();
}
For directions use this :
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" + lat
+ "," + lng + "&daddr="
+ AppointmentDetailLayout.docLatitude + ","
+ AppointmentDetailLayout.docLongitude));
context.startActivity(intent);

Open Inbuilt Map Intent For Driving Direction

I want to open an inbuilt google map intent to show route between two place without open a complete action dialog, which asked for Browser and Map .
I tried this.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+PreferenceData.getLatitude(HotelInfomation.this)+","+PreferenceData.getLongitude(HotelInfomation.this)+"&daddr="+latitude+","+longitude));
startActivity(intent);
But it opens a dialog asking Complete Action Using Browser or map . I don't want that Dialog .
without open a complete action dialog, which asked for Browser and Map
In this case you need to tell android which application you want to use to display map.
Here you go
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+PreferenceData.getLatitude(HotelInfomation.this)+","+PreferenceData.getLongitude(HotelInfomation.this)+"&daddr="+latitude+","+longitude));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="
+ Data.map_latitude + ","
+ Data.map_longitude + "+&daddr="
+ Data.infoLatitude + ","
+ Data.infoLogitute + ""));

Suppress chooser dialog when calling Google map using intent

I want to call google map intent without showing "Complete Action Dialog"?
Is it possible? Here is my code.
String uri = "http://maps.google.com/maps?saddr=" + Utils.getLatitude(ShowDetails.this)+","+Utils.getLongitude(ShowDetails.this)+"&daddr="+userLatitude+","+userLongitude;
startActivity(new Intent(android.content.Intent.ACTION_DEFAULT, Uri.parse(uri)));
I dont want to show below dialog when calling google map intent . Any Help is appreciated .
Below code helps me to solve my question.
String uri = "http://maps.google.com/maps?saddr=" + Utils.getLatitude(ShowDetails.this)+","+Utils.getLongitude(ShowDetails.this)+"&daddr="+userLatitude+","+userLongitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
Use the Google Maps URI rather than "http://[...] "
It goes something like this:
String uri = "geo:" + latitude + "," + longitude
Check out http://developer.android.com/guide/appendix/g-app-intents.html for the info.
Try ACTION_VIEW instead of ACTION_DEFAULT:
String uri = "geo:"+ latitude + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri)));
check this Android doc Intents List and browse to "Google Maps".

Categories

Resources