I am creating an app where it requires opening google maps to navigate to a place in walking mode and to set source/from location to current location.
Here is the code I use.
String url = "https://maps.google.co.in/maps?q=" + destination + "&mode=walking";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
How do I set "From location" to "current location"??
Thanks.
I would suggest using a Google Maps URLs instead of the link you provided in the example. The Google Maps URLs is an official and recommended way to launch Google Maps app on mobile devices and web browsers.
Please note that if you omit the origin parameter in the Google Maps URLs, the API will use your current location if available
origin: Defines the starting point from which to display directions. Defaults to most relevant starting location, such as user location, if available. If none, the resulting map may provide a blank form to allow a user to enter the origin. The value can be either a place name, address, or comma-separated latitude/longitude coordinates. A string should be URL-escaped.
So, you should rewrite your code as
String url = "https://www.google.com/maps/dir/?api=1&destination=" + destination + "&travelmode=walking&dir_action=navigate";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
I hope this helps!
Related
I have a place id obtained from Google Places API, and the aim is opening Google Maps app via place id like similar approach of below
Uri gmmIntentUri = Uri.parse(String.format(Locale.ENGLISH,"geo:%f,%f", lat, lon));
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
Is there a way for this ?
I have already latitude, longitude when user type for example "Eiffel Tower" from places API. And I'm storing those values for next usages(Say we are listing some sort of visited places from local database, and user click one of them and app navigates to Google Maps)
I have a few concerns
If i use latitude-longitude to indicate a place instead of place id
on Google Maps, what will happen if "Cafe XYZ" moved another street
?
If i use place id each time to receive last location of "Cafe XYZ"
(instead of stored values in my local database) the app easily hit request limits
So those concerns made me think a way that just sending place id Google Maps(like sending coordinates)
Indeed, there is a way to open Google Maps app with a place ID. For this purpose you have to use Google Maps URLs that was launched in May 2017. Following the documentation of Google Maps URLs you can construct the following URL for "Eiffel Tower" (place ID ChIJLU7jZClu5kcR4PcOOO6p3I0)
https://www.google.com/maps/search/?api=1&query=Eiffel%20Tower&query_place_id=ChIJLU7jZClu5kcR4PcOOO6p3I0
So, your code will be something like
Uri gmmIntentUri = Uri.parse("https://www.google.com/maps/search/?api=1&query=Eiffel%20Tower&query_place_id=ChIJLU7jZClu5kcR4PcOOO6p3I0");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
I hope this helps!
It's probably super late but I was able to make it work with info we should have aside from the LatLog
Searching for CenturyLink Field using latitude/longitude coordinates as well as the place ID results in the following map:
https://www.google.com/maps/search/?api=1&query=47.5951518,-122.3316393&query_place_id=ChIJKxjxuaNqkFQR3CK6O1HNNqY
I am using below code to open direction from current location to particular location in Google Maps.
But the code first shows me "Open With dialog" giving user choice to open with other components also. I want user to be taken directly to Google maps without giving him choice to select. How can I do that?
String uri = "https://maps.google.com/maps?f=d&daddr="+location;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
You can use the below format (not officially supported):
https://www.google.com/maps/dir/My%20location/DESTINATION_ADDRESS
OR
https://www.google.com/maps?saddr=My+Location&daddr=DESTINATION_ADDRESS
eg:
https://www.google.com/maps/dir/My%20location/world%20of%20coca%20cola
https://www.google.com/maps?saddr=My+Location&daddr=world%20of%20coca%20cola
Where DESTINATION_ADDRESS is the destination address you want to navigate to.
How can I launch route activity on google maps from my app without specifying the destination? I am using:
Uri gmmIntentUri = Uri.parse("google.navigation:q=<>");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Bad behavior :
But the "to" field in google maps has "<>" string, and if I write:
Uri gmmIntentUri = Uri.parse("google.navigation:q=");
But this not working.
The idea is:
Right behavior:
There doesn't seem to be a way for you to specifically go to a specific Activity in the official Maps app (empty destinations). If you look at the Google Maps Intent, it exposes several intents that can launch Maps to display, search, navigate and use Street View. For searching location, you are required to have coordinates or optional queries and labels.
I want to know if this is possible or now.
Right now I am opening the Google Maps with navigation from current user location to a defined set of coordinates like this:
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + String.valueOf(getDestinationLatitude()) + "," + String.valueOf(getDestinationLongitude())));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.driveabout.app.NavigationActivity");
startActivity(intent);
Is it possible to to have multiple sets of coordinates between current location and a final destination, so go to point a, point b and then finally destination point c. ?
It is possible to have multiple waypoints along the way to a certain destination. This can be achieved by using the Directions API. This is the API that would also give you the legs[] and the steps[] array which helps you in properly implementing and returning probable paths from source to the destination covering all the waypoints along the way.
For a sample implementation, check this link.
HI Im using a intent to send to a google maps a position via geo: with his latitude and longitude and mark a position with a titles. but know that i update the google maps v7.0.1 on android 4.2.2 it wont display the mark that i did in the code. here is the code that i used for make the call of maps.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:"+latitude+","+longitude+"?q="+latitude+","+longitude+"("+name+")"));
intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
startActivity(intent);
It seems that it is not supported any more.
Also this syntax is not mentioned in Google Uris, RFC 3986 or geo: Uri Draft.
If google maps does not find the given name at this point, there will be no search result. You can remove the name and google maps will draw a marker at the given point with its street name.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:"+latitude+","+longitude+"?q="+latitude+","+longitude+"("+name+")"));
intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
startActivity(intent);