我如何在应用程序中使用Intent打开Google Maps并导航到指定地址?我有地址,但没有纬度/经度。应该怎么做呢?谢谢。
我如何在应用程序中使用Intent打开Google Maps并导航到指定地址?我有地址,但没有纬度/经度。应该怎么做呢?谢谢。
使用以下代码:
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,+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复制。
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;
}
https://www.google.com/maps/search/?api=1&query=centurylink+field
希望这可以帮到你!
从当前文档 https://developers.google.com/maps/documentation/urls/get-started 最好使用 https://www.google.com/maps/dir/?api=1&query=address
所以:
val address = "一些地址"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/maps/dir/?api=1&query=$address"))
startActivity(intent)