How do I open google maps app on tapping an address? - android

I have an address param within my app say " 921 Beverly Hills, california, CA- 09001"
Upon tapping this I want google maps to open and show annotation for this exact address. How do I do that?
Any clue?

launch maps
There are great resources to get you going this is a sample. In a real app you would want to handle the intent with your own map activity; maybe add bounds and a camera to zoom in or whatever...
Location sf = new Location("");
sf.setLatitude(37.7749);
sf.setLongitude(-122.4194);
requestLocation("your address");
public void requestLocation(Location location) {
// Create a Uri from an intent string. Use the result to
// create an Intent.
Uri gmmIntentUri = Uri.parse("geo:" + location.getLatitude() + "," + location.getLongitude());
// Create an Intent from gmmIntentUri. Set the action to
// ACTION_VIEW
Intent mapIntent = new Intent(Intent.ACTION_VIEW,
gmmIntentUri);
// Make the Intent explicit by setting the Google Maps
// package
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(mapIntent, REQUEST_LOCATION);
}
}
public void requestLocation(String address) {
// Create a Uri from an intent string. Use the result to create
//an Intent.
Uri gmmIntentUri = Uri.parse("geo:" + address);
// Make the Intent explicit by setting the Google Maps
// package
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(mapIntent, REQUEST_LOCATION);
}
}

Here you can find an overview on how to use intents to start Google Maps with an Address:
https://developers.google.com/maps/documentation/urls/android-intents

Related

how to Intent to particular location in google maps with marker on it?

I'm creating a tour app with Firebase as a backend. I need to know how to intent aparticular location with marker on it?
Intent i = new Intent (Intent.ACION_VIEW) like this, or is there any other method to do with marker on it ?
Try this sample for launching google maps with specific Lat & Lng
public void gotToLocation(String markerName,Float latitude, Float longitude ) {
String uri = String.format(Locale.ENGLISH, "geo:0,0?q=%f,%f(%s)", latitude,longitude,markerName);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));;
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(mapIntent);
}
}
then call it like this
gotToLocation("MyLocation",34.99,-106.61);
Hi you can use google documentation.
Google Map
Uri gmmIntentUri = Uri.parse("geo:0,0?q=-33.8666,151.1957(Google+Sydney)");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Above example displays a label at the location of Google's Sydney office

how to fix application terminate after opening google map with directions from my custom android app

After i open google map activity with direction from my custom fragment it tend to terminate the whole app.
mapView.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+currlat+","+currLon+"&daddr="+destinationLat+","+destinationLon+"&travelmode=driving")));
in here mapView means my custome fragmnet.
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" + lastLocation.getLatitude() + "," + lastLocation.getLongitude() + "&daddr=" +
destinationLocationLatitude + "," +
destinationLocationLongitude));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
There is this example from the google developer doc where you use a geo: URI instead of an url
This URI works like this :
Use the geo: intent to display a map at a specified location and zoom level.
geo:latitude,longitude?z=zoom
Here is a code that uses it. Also, it ensures that Google Maps will receive it and not another app, and if Maps isn't installed, it just does nothing but you can add an else statement to tell the user that he needs Maps to execute the action
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
To launch navigation, use google.navigation:q=latitude,longitude. The starting position will be the user current location. See this part of the google developer site
Uri gmmIntentUri = Uri.parse("google.navigation:q=37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}

How can I ask the user for Yandex Navi or Google Maps? How can I direct it?

I have two values in latitude and longitude. I have a key. This button must have two options to go to location information,Yandex Navi and Google Maps. When I click on the button, I want to know which one would like to open it. How can I do that?
You can use Intent.createChooser() like:
String url = "yandexmaps://maps.yandex.ru/?pt=" + latitude + "" + longitude + "&z=12&l=map";
Intent intentYandex = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intentYandex.setPackage("ru.yandex.yandexmaps");
String uriGoogle = "geo:" + latitude + "," + longitude;
Intent intentGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse(uriGoogle));
intentGoogle.setPackage("com.google.android.apps.maps");
String title = "Select";
Intent chooserIntent = Intent.createChooser(intentGoogle, title);
Intent[] arr = new Intent[1];
arr[0] = intentYandex;
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr);
startActivity(chooserIntent);
Andrii Omelchenko's answer always opens Google Maps for me. But after change google and Yandex order for chooserIntent, it is worked in my case:
val uriYandex = "yandexnavi://build_route_on_map?lat_to=${latitude}&lon_to=${longitude}"
val intentYandex = Intent(Intent.ACTION_VIEW, Uri.parse(uriYandex))
intentYandex.setPackage("ru.yandex.yandexnavi")
val uriGoogle = Uri.parse("google.navigation:q=${latitude},${longitude}&mode=w")
val intentGoogle = Intent(Intent.ACTION_VIEW, uriGoogle)
intentGoogle.setPackage("com.google.android.apps.maps")
val chooserIntent = Intent.createChooser(intentYandex, title)
val arr = arrayOfNulls<Intent>(1)
arr[0] = intentGoogle
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arr)
val activities = packageManager.queryIntentActivities(chooserIntent, 0)
if(activities.size>0){
startActivity(chooserIntent)
}else{
//do sth..
}
If it absolutely has to be either Google Maps or Yandex Navi, the easiest way would probably be to figure out which one the user wants to use (via a dialogue or similar), and then set that as the target of a Map Intent. For example, here's an intent from Google's Android documentation that targets Google Maps by app name:
// Creates an Intent that will load a map of San Francisco
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW,
gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
This should also work with Navi by setting the package to "ru.yandex.yandexnavi".
Note, though, that a more standard way to do this would be with a Map Intent that doesn't specify the targeted app. That way, all you would have to provide is the coordinates, and then the user could use their app of choice:
public void showMap(Uri geoLocation) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(geoLocation);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

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.

open google maps through intent for specific location in android

I'm designing one application in which I want to show specific location on Map.
I'm passing String of address which is already placed on Google Map.
Following is my Intent code..
String url = "http://maps.google.com/maps?daddr="+address;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
But it gives me Google Map for getting direction. I know why that so, because I used daddr in url but I don't know what to use for specific location..Please tell me what to use there..
I have not tested this but you could try :
First method:
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
EDIT:
This might not work with Google maps 7,0
hence you could change the uri to :
Second option:
String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";
where mTitle is the name of the location.
Third option:
geo:0,0?q=my+street+address
Fourth option:
String map = "http://maps.google.co.in/maps?q=" + yourAddress;
Hope that works and helps :D..
Get Lat-Lng Using this web-service
http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false
Then Pass it to this code
String strUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + "Label which you want" + ")";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
I hope it will help you
Thank you.
The latest version of map provides better solution. If you wish show an address on map use below code.
Uri mapUri = Uri.parse("geo:0,0?q=" + Uri.encode(address));
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
If you want to display using latitude and longitude values, use below method.
Uri mapUri = Uri.parse("geo:0,0?q=lat,lng(label)");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Lat and lng being the latitude and longitude you wish to display, the label here is optional.
This will work like a charm. Make sure to check for existence for Google Maps, so this can work universally across all non Google devices also. It will open in a browser in such cases.
Also, remember, don't have www in the URL. Else this will not work.
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?q=loc:" + latitude + "," + longitude));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Only if initiating from a Broadcast Receiver
String mapsPackageName = "com.google.android.apps.maps";
if (isPackageExisted(context, mapsPackageName)) {
i.setClassName(mapsPackageName, "com.google.android.maps.MapsActivity");
i.setPackage(mapsPackageName);
}
context.startActivity(i);
private static boolean isPackageExisted(Context context, String targetPackage){
PackageManager pm=context.getPackageManager();
try {
PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
As of 2020 you can also consider using Google Maps URLs, the API designed by Google in order to create universal cross-platform links. You can open Google maps on web, Android or iOS using the same URL string in form:
https://www.google.com/maps/search/?api=1&parameters
In case when you need show certain location you can use an URL like
https://www.google.com/maps/search/?api=1&query=36.26577,-92.54324
The code snippet is
String url = "https://www.google.com/maps/search/?api=1&query="+address;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
Refer this documentation
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=" + src_lat+ "," + src_lng + "&daddr=" + des_lat + "," + des_lng));
startActivity(intent);
}catch (ActivityNotFoundException ane){
Toast.makeText(activity, "Please Install Google Maps ", Toast.LENGTH_LONG).show();
}catch (Exception ex){
ex.getMessage();
}
}
});
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address));
startActivity(intent);
This helper method using uriBuilder for cleaner code and handle condition if there is no activity on device that can open map
public static boolean openMap(Context context, String address) {
Uri.Builder uriBuilder = new Uri.Builder()
.scheme("geo")
.path("0,0")
.appendQueryParameter("q", address);
Intent intent = new Intent(Intent.ACTION_VIEW, uriBuilder.build());
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
return true;
}
return false;
}
I find this solution in Google documentation so, it will work fine.
Double latitude = 37.7749,longitude = -122.4192;
String label = "Your location desired Aryan";
Uri gmmIntentUri =
Uri.parse("geo:0,0?q="+latitude+","+longitude+"("+label+")");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
context.startActivity(mapIntent);
If you have any doubt just comment it. I am not checking maps package because on every Android device we have maps. If you want you check it easily.
For the ones, who are looking for opening the "Maps" app for a particular location(by passing lat and long) and do not want that lat/long co-ordinates to be shown in Maps search bar after redirection(Opening Maps app). Instead wish to show some label or place name, you can refer to the code below,
private void openMapView(String latitude , String longitude , String locationName){
Uri gmmIntentUri = Uri.parse("geo:"+latitude+","+longitude+"?q="+locationName);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(mapIntent);
}
}
Note : "locationName" in the method above, represents the name you wish to display inside the Maps search bar.
To show location and disply show directions button inside google Maps use
this code snippet:
String geoUri = "http://maps.google.com/maps?q=loc:" + latitude + "," + longitude + " (" + locationName + ")";
Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(mapIntent);
I thought I would chip in with the Kotlin code.
I created a helper function that takes latitude, longitude and context params to fire off the intent.
fun openGoogleMap(latitude: Double, longitude: Double, context: Context) { ... }
Inside the openGoogleMap() helper function create an intent passing in the action, data to act upon (lat/long) and package name for the Google Map application.
fun openGoogleMap(latitude: Double, longitude: Double, context: Context) {
// you need action, data and package name
val uri = Uri.parse("geo:$latitude,$longitude?z=15")
//create an intent
val mapIntent = Intent()
//add action & data
mapIntent.action = Intent.ACTION_VIEW
mapIntent.data = uri
//package name for google maps app
mapIntent.setPackage("com.google.android.apps.maps")
try {
context.startActivity(mapIntent)
} catch (e: ActivityNotFoundException) {
//if map app isn't resolved/installed catch error
e.printStackTrace()
}
}
The docs say that
To verify that an app is available to receive the intent, call
resolveActivity() on your Intent object. If the result is non-null,
there is at least one app that can handle the intent and it's safe to
call startActivity().
But I found that using resolveActivity() has issues when used with this code especially when you are outside the Activity's code.
mapIntent.resolveActivity(packageManager)?.let {
...
}
To get around this I used try-catch block to catch ActivityNotFoundException with startActivity().
mapIntent.setPackage("com.google.android.apps.maps")
try {
context.startActivity(mapIntent)
} catch (e: ActivityNotFoundException) {
//if map app isn't resolved/installed catch error
e.printStackTrace()
}
I tested the util function and it does work and is launching the GoogleMaps with Kotlin code.
If you have the name and address of the location available then you can launch the exact spot along with its images, details and navigation option using the following code :
fun loadLocation(ctx: Context, name: String, address: String) {
try {
val geoUri = "http://maps.google.com/maps?q=$name,$address"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(geoUri))
ctx.startActivity(intent)
}
catch (ex: Exception) {
ex.printStackTrace()
}
}
https://www.google.com/maps/dir//37.4219983,-122.084
String location = "https://www.google.com/maps/dir//" + latitude + "," + longitude;
try {
SmsManager sms = SmsManager.getDefault(); // using android SmsManager
sms.sendTextMessage(number, null, message + location, null, null); // adding number and text
Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Sms not send, please check phone number/network", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}

Categories

Resources