How to open Google Maps using address?
Asked Answered
D

8

21

How can I open Google Maps(using Intents or adding Google Maps into my application) with address? I have the address, but I don't have latitude/longitude. How can I do it? Thank you.

Devastating answered 3/4, 2012 at 5:25 Comment(0)
P
50

use below code,

String map = "http://maps.google.co.in/maps?q=" + str_location; 

// where str_location is the address string

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(map));
        startActivity(i);
Press answered 3/4, 2012 at 5:34 Comment(0)
A
16

From my personal Code Library. ;)

public static Intent viewOnMap(String address) {
    return new Intent(Intent.ACTION_VIEW,
                      Uri.parse(String.format("geo:0,0?q=%s",
                                              URLEncoder.encode(address))));
}

public static Intent viewOnMap(String lat, String lng) {
    return new Intent(Intent.ACTION_VIEW,
                      Uri.parse(String.format("geo:%s,%s", lat, lng)));
}
Antemortem answered 3/4, 2012 at 5:27 Comment(0)
I
5
String geoUri = "http://maps.google.com/maps?q=loc:" + addressName;
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
            context.startActivity(intent);
Isolationist answered 18/7, 2019 at 9:29 Comment(0)
W
1

Change the bold part of this URL to your company address. It's best if you replace all spaces with a plus (+) character, but should work with spaces too: http://maps.google.com/maps/geo?q=620+8th+Avenue,+New+York,+NY+10018,+USA&output=csv&oe=utf8&sensor=false

Raise a request to above URL. For more information refer http://androidadvice.blogspot.in/2010/09/asynchronous-web-request.html

This will generate a code that looks something like this:

200,8,40.7562008,-73.9903784

The first number, 200, says that the address is good. The second number, 8, indicates how accurate the address is. The last two numbers, 40.7562008 and -73.9903784, are the latitude and longitude of this address. Use these to get your google map working.

Note : The above steps have been copied from http://webdesign.about.com/od/javascript/ss/add-google-maps-to-a-web-page_2.htm

Whall answered 3/4, 2012 at 5:35 Comment(0)
E
0

You can use Google Geocoding API, which converts your physical address into latitude and longitude. API returns it into XML or JSON format. You just need to parse the data to get latitude and longitude. After receiving latitude and longitude you can load it on mapview.

Geocoding api link :

https://developers.google.com/maps/documentation/geocoding/

Hope this helps.

Elodia answered 3/4, 2012 at 5:33 Comment(0)
O
0

As of 2017 the recommended by Google approach is using the Google Maps URLs API that provides universal cross-platform URLs. You can use these URLs in your intents.

Example of such URL from the documentation:

https://www.google.com/maps/search/?api=1&query=centurylink+field

Hope this helps!

Onstage answered 2/8, 2017 at 10:20 Comment(0)
L
0

Little late to the party. I prefer @st0le's answer but URLEncoder.encode(String s) is deprecated as of API 16. You need to pass a second argument as well. Check the answer below.

 public static Intent viewOnMapA(String address) {
    try {
        return new Intent(Intent.ACTION_VIEW,
                Uri.parse(String.format("geo:0,0?q=%s",
                        URLEncoder.encode(address, "UTF-8"))));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}
Lashondalashonde answered 23/6, 2018 at 9:14 Comment(0)
R
0

From the current documentation https://developers.google.com/maps/documentation/urls/get-started It is better to use https://www.google.com/maps/dir/?api=1&query=address

so:

val address = "some address"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps/dir/?api=1&query=$address")) startActivity(intent)

Raddle answered 9/6, 2021 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.