目前我正在开发一个Android应用程序。 根据我的收集数据,我有一个位置列表(不是地址) - 只有位置名称(例如餐馆名称,度假村,海滩......),我想在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));
答案 0 :(得分:4)
有一个Geocoder类,您可以使用它来按位置名称String
查找地址。方法是
Geocoder gc = new Geocoder(context);
List<Address> addresses = gc.getFromLocationName(String locationName, int maxResults);
这将为您提供Address
个对象的列表。 Address
包含方法getLongitude()
和getLatitude()
等。
由于您的地图已嵌入,GoogleMap
类有一个方法moveCamera(...)
可以找到该位置,如果找到了您的位置名称。
例如:
String locationName = "Nha Hang restaurant";
Geocoder gc = new Geocoder(context);
List<Address> addressList = gc.getFromLocationName(locationName, 5);
然后使用地址或获取经度和纬度,使用CameraUpdate
创建CameraUpdateFactory
。如果您愿意,可以尝试moveCamera(...)
。
答案 1 :(得分:1)
如果您无法访问纬度和经度,则必须使用Google Places api。使用此功能,您可以获得具有该名称的列表地址。
http://examples.javacodegeeks.com/android/android-google-places-autocomplete-api-example/
如果您已经拥有该位置的纬度和经度,则可以使用反向地理编码来查找位置地址。
Geocoder gc = new Geocoder(context);
if(gc.isPresent()){
List<Address> list = gc.getFromLocation(37.42279, -122.08506,1);
Address address = list.get(0);
StringBuffer str = new StringBuffer();
str.append("Name: " + address.getLocality() + "\n");
str.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
str.append("Admin Area: " + address.getAdminArea() + "\n");
str.append("Country: " + address.getCountryName() + "\n");
str.append("Country Code: " + address.getCountryCode() + "\n");
String strAddress = str.toString();
}