如何按位置名称查找地址(android google map)

时间:2015-07-17 07:05:19

标签: java android google-maps

目前我正在开发一个Android应用程序。 根据我的收集数据,我有一个位置列表(不是地址) - 只有位置名称(例如餐馆名称,度假村,海滩......),我想在Google Map API上找到每个位置的地址,如下所示图片: enter image description here

我怎么做?

在这种情况下,我感谢你的帮助。感谢。

我也在寻找一种方法,但我只找到了从特定地址或相反的方式获得纬度或经度的解决方案。

我实现了以下代码,但不能返回结果:(

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

2 个答案:

答案 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();
}

https://androidcookbook.com/Recipe.seam?recipeId=1454