31

Map Intent not working with specific zoom level as well as custom marker

    float lat = 40.714728f;
    float lng = -73.998672f;

    String maplLabel = "ABC Label";
    final Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
            Uri.parse("geo:0,0?q="+lat+","+lng+"&z=16 (" + maplLabel + ")"));
    startActivity(intent);

Anybody know what is wrong? or how to do so? I want to show map of certain (lat,lng) with a custom label-marker at a specific zoom level.

umirza47
  • 940
  • 2
  • 10
  • 21
  • Tested this and it works fine.. you sure you didn't leave startActivity commented out in your actual code? – dymmeh Oct 24 '12 at 16:30
  • Sorry, my mistake, I forgot to un-comment in this post. I tested it and It shows Unable to load URL. how did you do that? – umirza47 Oct 24 '12 at 16:35
  • I want to show this location at zoom level 16 with marker, It worked for location and marker but when I applied z parameter then It wont work at all – umirza47 Oct 24 '12 at 16:40
  • When pressing back button from maps app,there shows a black screen and underlying activity is recreated.Any idea how to fix this? – Jas Oct 14 '15 at 05:04

4 Answers4

91

Try the following solution:

double latitude = 40.714728;
double longitude = -73.998672;
String label = "ABC Label";
String uriBegin = "geo:" + latitude + "," + longitude;
String query = latitude + "," + longitude + "(" + label + ")";
String encodedQuery = Uri.encode(query);
String uriString = uriBegin + "?q=" + encodedQuery + "&z=16";
Uri uri = Uri.parse(uriString);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);

Credit goes here: Answer

I believe the problem had to do with the spaces in your label. Encoding the query string will eliminate the issue by replacing the spaces with valid characters

Community
  • 1
  • 1
dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • When pressing back button from maps app,there shows a black screen and underlying activity is recreated.Any idea how to fix this? – Jas Oct 14 '15 at 05:03
  • @dymmeh This answer is 4 years old now. I am unable to get this to work now. I posted a new [question](http://stackoverflow.com/q/41131414/7292819) but haven't been able to attract much interest except for someone to say this doesn't work starting with Maps 7.x. Do you know anything about changes in Maps or other reasons this might not work now? – Gary99 Dec 21 '16 at 17:53
  • I was missing `?q=`. If you want the marker, you MUST provide the query parameter. – NecipAllef May 17 '18 at 21:00
2

http://developer.android.com/guide/components/intents-common.html#Maps

It has pretty much everything related to the Maps intent.

Naren
  • 2,706
  • 1
  • 21
  • 15
1

Show location in maps application:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String data = String.format("geo:%s,%s", latitude, longitude);
if (zoomLevel != null) {
    data = String.format("%s?z=%s", data, zoomLevel);
}
intent.setData(Uri.parse(data));
startActivity(intent);
1
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=" + location.getLatitude() + "," + location.getLongitude()));
startActivity(intent);