目前我正在开发一个安卓应用程序。 从我收集的数据中,我有一个位置列表(不是地址) - 只有位置名称(例如餐厅,度假村,海滩的名称......),我想在 Google Map API 上找到每个位置的地址,如下所示图片:
我该怎么做?
非常感谢您在此案例中提供的帮助。谢谢。
我也在寻找一种方法,但我只找到了从特定地址或相反获取纬度或经度的解决方案。
我实现了下面的代码但是没有返回结果:(
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
String locationName = "Nhà hàng Blanchy Street, VietNam";
Geocoder gc = new Geocoder(this);
List<Address> addressList = coder.getFromLocationName(locationName, 5);
Address location = addressList.get(0);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng sydney = new LatLng(latitude , longitude );
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
最佳答案
有一个Geocoder类,您可以使用它按位置名称 String
查找地址。方法是
Geocoder gc = new Geocoder(context);
List<Address> addresses = gc.getFromLocationName(String locationName, int maxResults);
这将为您提供 Address
对象的列表。 Address
具有方法 getLongitude()
和 getLatitude()
等。
并且由于您的 map 是嵌入的,GoogleMap
类有一个方法 moveCamera(...)
可以前往该位置(如果找到包含您的位置名称的位置)。
例如:
String locationName = "Nha Hang restaurant";
Geocoder gc = new Geocoder(context);
List<Address> addressList = gc.getFromLocationName(locationName, 5);
然后使用地址或获取经度和纬度以使用 CameraUpdateFactory
创建 CameraUpdate
。如果您愿意,可以尝试使用 moveCamera(...)
。
关于java - 如何通过位置名称查找地址(android google map),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31470160/