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

2

6

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?

Kress answered 13/2, 2012 at 7:14 Comment(0)
W
17

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);
Walford answered 13/2, 2012 at 8:8 Comment(2)
Thanks. This URI protocol seems to be undocumented, any idea about version compatibility?Kress
Note this won't work if you forget to add the parenthesis around the location name (Maninagar in this example)Richardricharda
B
-2

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.

Blocky answered 13/2, 2012 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.