user1166635
user1166635

Reputation: 2801

How to open Google Maps using address?

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.

Upvotes: 21

Views: 25106

Answers (8)

dkoukoul
dkoukoul

Reputation: 61

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)

Upvotes: 0

Ashwini
Ashwini

Reputation: 703

String geoUri = "http://maps.google.com/maps?q=loc:" + addressName;
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
            context.startActivity(intent);

Upvotes: 5

Fawzan
Fawzan

Reputation: 4849

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;
}

Upvotes: 0

xomena
xomena

Reputation: 32198

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!

Upvotes: 0

Maulik J
Maulik J

Reputation: 2765

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);

Upvotes: 50

AYK
AYK

Reputation: 3322

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

Upvotes: 1

hp.android
hp.android

Reputation: 2824

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.

Upvotes: 0

st0le
st0le

Reputation: 33575

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)));
}

Upvotes: 16

Related Questions