如何使用地址打开 Google 地图(使用 Intents 或将 Google 地图添加到我的应用程序中)?我有地址,但我没有纬度/经度。我该怎么做?谢谢你。
8 回答
使用下面的代码,
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);
来自我的个人代码库。;)
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)));
}
String geoUri = "http://maps.google.com/maps?q=loc:" + addressName;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
context.startActivity(intent);
将此 URL 的粗体部分更改为您的公司地址。最好用加号 (+) 字符替换所有空格,但也应使用空格: http ://maps.google.com/maps/geo? q= 620+8th+Avenue,+New+York,+ NY+10018,+美国&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
您可以使用 Google Geocoding API,它将您的物理地址转换为纬度和经度。API 将其返回为 XML 或 JSON 格式。您只需要解析数据以获取纬度和经度。收到纬度和经度后,您可以将其加载到地图视图上。
地理编码api链接:
https://developers.google.com/maps/documentation/geocoding/
希望这可以帮助。
从当前文档 https://developers.google.com/maps/documentation/urls/get-started 最好使用 https://www.google.com/maps/dir/?api=1&query=address
所以:
val address = "some address"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps/dir/?api=1&query=$address")) startActivity(intent)
截至 2017 年,Google 推荐的方法是使用提供通用跨平台 URL的Google Maps URL API。您可以在您的意图中使用这些 URL。
文档中此类 URL 的示例:
https://www.google.com/maps/search/?api=1&query=centurylink+field
希望这可以帮助!
晚会有点晚了。我更喜欢@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;
}