如何使用地址打开谷歌地图?

21

我如何在应用程序中使用Intent打开Google Maps并导航到指定地址?我有地址,但没有纬度/经度。应该怎么做呢?谢谢。

8个回答

49

使用以下代码:

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

// 其中 str_location 是地址字符串

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(map));
        startActivity(i);

16

来自我的个人代码库。 ;)

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

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

1

将此URL中的粗体部分更改为您公司的地址。最好用加号(+)字符替换所有空格,但也可以使用空格: http://maps.google.com/maps/geo?q=620+8th+Avenue,+New+York,+NY+10018,+USA&output=csv&oe=utf8&sensor=false

向上述URL发出请求。有关更多信息,请参见http://androidadvice.blogspot.in/2010/09/asynchronous-web-request.html

这将生成类似于以下内容的代码:

200,8,40.7562008,-73.9903784

第一个数字200表示地址是正确的。第二个数字8表示地址的准确度。最后两个数字40.7562008和-73.9903784是该地址的纬度和经度。使用这些来让你的谷歌地图工作。

注意:以上步骤已从http://webdesign.about.com/od/javascript/ss/add-google-maps-to-a-web-page_2.htm复制。


0
来晚了。我喜欢@st0le的答案,但URLEncoder.encode(String s)自API 16起已被弃用。你需要传递第二个参数。请查看下面的答案。
 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;
}

0
截至2017年,Google推荐的方法是使用Google Maps URLs API,该API提供通用的跨平台URL。您可以在您的意图中使用这些URL。
下面是文档中的一个示例URL:

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

希望这可以帮到你!


0

0
您可以使用Google Geocoding API,将您的物理地址转换为纬度和经度。API以XML或JSON格式返回它。您只需要解析数据以获取纬度和经度。在收到纬度和经度后,您可以将其加载到地图视图中。
地理编码API链接:

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

希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接