java - 如何通过位置名称查找地址(android google map)

标签 java android google-maps

目前我正在开发一个安卓应用程序。 从我收集的数据中,我有一个位置列表(不是地址) - 只有位置名称(例如餐厅,度假村,海滩的名称......),我想在 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));

最佳答案

有一个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/

相关文章:

android - 我将如何在 Retrofit 中处理带有破折号/连字符的 JSON 键?

android - 如何在android中构建键盘提示布局

android - Gradle 项目同步失败。 Unresolved 依赖关系

java - java 中 log4j2 jdbc 附加程序的自定义日志记录整数值

java - 预 Controller 过滤器的 ExceptionHandler (Spring Security)

google-maps - Google Road Api SnapToRoad golang

java - 更新后如何获取位置?

javascript - 添加特定于 Windows Phone Internet Explorer 的代码

java - 由 : java. lang.NoClassDefFoundError : when class exists under tomcat/lib? 引起

java - 在 Java 中获取 SSLHandshakeException