Start Google Map Intent - android

hi i wanted to make a button which is the button will start google map intent
with my current location and destination location
how to make a google map automaticlly detect my location and destination longitude and lat?
where i put that longi and lat?
i want that map directly draw the direction to destination
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?"
+ "saddr="+ current_lat+","+current_longi + "&daddr="+dest_address ));
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
alr tried this code but not working

Try using one of these with the View intent:
geo:latitude,longitude
geo:latitude,longitude?z=zoom
geo:0,0?q=my+street+address
geo:0,0?q=business+near+city
Something like:
Uri uri = Uri.parse(<ONE OF THE ABOVE STRINGS>);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);

Try this one:
String url = "http://maps.google.com/maps?saddr="+currentLattitude+","+currentLongitude+"&daddr="+targetLat+","+targetLang+"&mode=driving";
it should work.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=srcLatitude,srcLongitude&daddr=destLatitude,destLongitude"));
startActivity(intent);
// srcLongitude/Latitude = current Location`s longitude/latitude
// destLongitude/Latitude = Destination Location`s longitude/latitude

Related

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.

How to pass a list of coordinates to the default google Map Application?

From my app i am opening the default google map app. Here is how i am doing it.
String uri = String.format(Locale.ENGLISH, "geo:0,0?q=%f,%f(%s)",
locationList.get(1).getLatitude(), locationList.get(1).getLongitude(),"My location");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
Log.i(TAG, "Uri is "+ uri);
this.startActivity(intent);
This is working fine. But is it possible to pass a list of coordinates and plot them all so i could create a path?
Here try this:
String destination = locationList.get(1).getLatitude()+","
+locationList.get(1).getLongitude();
Uri gmmIntentUri = Uri.parse("google.navigation:q="+destination+"&mode=d");
Log.e(TAG,"Intent uri : "+gmmIntentUri.toString());
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mapIntent.setPackage("com.google.android.apps.maps");
this.startActivity(mapIntent);
this will open map in navigation mode from your current location to the specified destination.
you can have a look at the below link to specify way points between your source and destination:
https://developers.google.com/maps/documentation/urls/android-intents
https://developers.google.com/maps/documentation/urls/guide
Let me know if you have any doubts.

How to stop google MapsActivity from rerouting my route?

I am using
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
for providing navigation support in my app , but whenever I go off route than it draws a new route to the destination. How to stop it, I want the route to be fixed

Intent to Google Navigation for itinerary with several waypoints

I want to know, if it's possible to put several "checkpoints" within "Google Navigation" (Google Maps Navigation) With a query that follows the next syntax : "google.navigation:q=44.871709,-0.505704...."
If it possible, what is the syntax for separate two points ?
For one point it works, example : "google.navigation:q=44.871709,-0.505704"
But I would put a few checkpoints for example :
LatLng point1 = new LatLng(44.871709,-0.505704);
LatLng point2 = new LatLng(43.572665,3.871447);
Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.navigation:q="+
point1.latitude+","+point1.longitude+";"+ point2.latitude+","+point2.longitude));
I read others issues, they say to use this syntax :
"http://maps.google.com/maps?saddr="+"44.871709,-0.505704"+"&daddr="+"43.572665,3.871447"
With above solution, "Google Navigation" is not started automatically, we must choose one application "Google Maps" and next, to click on Itinerary (in top of the screen) to start "Google Navigation".
I would like to avoid it if possible.
Here is a way to supress the dialog and go to maps directly.
String uri = "http://maps.google.com/maps?saddr="+"44.871709,-0.505704"+"&daddr="+"43.572665,3.871447";
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);
Let me know if you still have issues.
UPDATE
As of May 2017, there is a better approach, the Google Maps URLs API
https://developers.google.com/maps/documentation/urls/guide
Using this API you can construct an URL with origin, destination and waypoints and pass it to the intent
String uri = "https://www.google.com/maps/dir/?api=1&origin=Madrid,Spain&destination=Barcelona,Spain&waypoints=Zaragoza,Spain%7CHuesca,Spain&travelmode=driving&dir_action=navigate";
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);
Check these 2 methods, both of them work properly for me.
METHOD 1:
String packageName = "com.google.android.apps.maps";
String query = "google.navigation:q=49.4839509,8.4903999";
Intent intent = this.getPackageManager().getLaunchIntentForPackage(packageName);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(query));
startActivity(intent);
METHOD 2:
String packageName = "com.google.android.apps.maps";
String query = "google.navigation:q=49.4839509,8.4903999";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(query));
intent.setPackage(packageName);
startActivity(intent);
Because point1.longitude is Double,you should use this
String.valueOf(point1.latitude);
Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.navigation:q="+String.valueOf(point1.latitude)+","+String.valueOf(point1.longitude)+";"+String.valueOf( point2.latitude)+","+String.valueOf(point2.longitude)));
Or you can use this,but it is just the same.
point1.toString().replace("lat/lng: (", "").replace(")", "")
point1.toString() the result is "lat/lng: (44.871709,-0.505704)",remove the "lat/lng: (" and ")".
then you can get the result that you want.
Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+point1.toString().replace("lat/lng: (", "").replace(")", "")+"&daddr="+point2.toString().replace("lat/lng: (", "").replace(")", "")));
Or you can try this
Uri uri = Uri.parse("http://maps.google.com/mapssaddr="+point1.toString().replace("lat/lng: (", "").replace(")", "")+"&daddr="+point2.toString().replace("lat/lng: (", "").replace(")", ""));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
I hope this can help you.
You have to just pass destination location. Google Maps app will automatically take your current location for navigation.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + String.valueOf(destination latitude)
+ "," + String.valueOf(destination longitude)));
startActivity(intent);

How to launch map intent with a marker/overlay item at given latitude and longitude?

I have a latitude and longitude and I want to open google maps centred at that point. So i use the following code for it:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:"+lat +","+lng));
startActivity(intent);
However, this does not place a marker on the center. I want to place some kind of marker at the the point I start with (and optionally some kind of name for it). Any idea how this can be achieved?
You can use it like,
Showing Particular Place,
Uri uri = Uri.parse("geo:0,0?q=22.99948365856307,72.60040283203125
(Maninagar)");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Showing Route between to Places,
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr="+23.0094408+","+72.5988541+"&
daddr="+22.99948365856307+","+72.60040283203125));
startActivity(intent);
You can simply put latitude and longitude as extras to the intent like this:
Intent intent = new Intent();
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);
And then fetch them from the MapActivity you've started:
Intent intent = getIntent();
double latitude = intent.getDoubleExtra("latitude", 0.0);
double longitude = intent.getDoubleExtra("longitude", 0.0);
Then you can use those values for anything you want. Hope this helps.

Categories

Resources