How to open Google Maps using address?

跟風遠走 提交于 2019-12-04 17:56:45

问题


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.


回答1:


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



回答2:


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



回答3:


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




回答4:


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.




回答5:


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!




回答6:


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



回答7:


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


来源:https://stackoverflow.com/questions/9987551/how-to-open-google-maps-using-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!