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.
Related
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!
I want my user to enter an address. But I want the address to be confirmed with Google Map. So when the user clicks on a button to enter the address, I want to launch an Intent for Google Map where user can enter the address. But then I want the map to return the data to me. How do I do that? If I start my activity as
Uri searchUri = Uri.parse("geo:0,0?z=10&q=");
Intent mapIntent = new Intent(Intent.ACTION_VIEW,searchUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivityForResult(mapIntent,RESULT_CODE_LOCATION);
number one, will it do what I seek? Second: how do I implement onActivityResult? I need the human readable address and the lat-long.
To be clear: I want the user to enter the address through Google Map and then have the address returned to my app.
I figure it out. I have to use PlacePicker to accomplish what I seek. https://developers.google.com/places/android-api/placepicker
I'm opening a google map using my android program. I did it with the following URL,
http://maps.google.com/maps? saddr=31.186371,121.489885&daddr=31.249351,121.45905&mode=driving
But this opens a view like,
I want to open the shortest path(1st one in list view) from these paths. Is there a way to do this? How may I do this?
According to the documentation, you can launch the turn by turn navigation by this intent:
google.navigation:q=a+street+address
you can use these code to launch the intent:
Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
As you might able to tell, this can only give a turn-by-turn direction from current location to point A, but not point A to point B. You should consider using the web direction API and draw the polyline on your own Google Maps to gain more controls.
The below code is run on an AVD. The AVD browser opens the link through an application and shows google maps, but doesn't display the marker. The browser has the below 'String url' in it's url. When I press enter the url goes to the exact location and creates the marker location. I want to have the browser automatically bring me to the marker location and display a marker without having to click on the url and press enter. Any help is appreciated.
String url = "https://maps.google.com/maps?z=10&t=m&q=loc:"+latitude+"+"+longitude+"";
Uri uri = Uri.parse(Uri.encode(url));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Removed the Uri.encode(url) and it seems to work OK now.
I want to draw a path on map between known points(lat, long) using:
String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+source_latitude+","+source_longitude+"&daddr="+destination_latitude+","+destination_longitude;
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);
But instead of just drawing route, Google Maps shows menu Car/Bus/By foot, trying to make a route from my current location to those points. What am I doing wrong? Is it still possible to draw route on map using the method above?
By running this intent you are basically opening an "outside" application - Google Maps that is not related to your application. the behavior you are receiving is the behavior that should be for the version of Google Maps you have in your phone. You can't edit the way it behaves, only the things that you are allowed to change using the settings screen of this application.
You have to parse the xml or json that your are using in the url in an Asynctask class and draw it on the map.