【问题标题】:How to get the place address from specified latitude and longitude with Google Place API如何使用 Google Place API 从指定的纬度和经度获取地点地址
【发布时间】:2018-08-31 09:48:01
【问题描述】:

我正在制作像 Uber 这样的出租车预订应用程序, 用户拖动地图以使用图钉选择他的位置,

然后我抓住了那个大头针的 LatLng。

这是我的代码:

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
        @Override
        public void onCameraIdle() {
            pinLocation = mMap.getCameraPosition().target;
            setPickupLocationPrefs(pinLocation);
        }
    });

    //Initialize Google Play Services
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            updateLocationUI();
        }
    }
    else {
        buildGoogleApiClient();
        updateLocationUI();
    }
}

我想获取该 pin 的位置地址,以向我的用户显示应用程序是否在该位置运行(如 uber 那样)。

如何从引脚位置坐标中获取该地址?

【问题讨论】:

    标签: android google-maps google-places-api google-maps-android-api-2


    【解决方案1】:

    使用反向地理编码。首先从针点获取纬度和经度。最好在后台线程中处理这个。否则会阻塞 UI。

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder gc = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = gc.getFromLocation(lat, lng, 1);
        StringBuilder sb = new StringBuilder();
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                sb.append(address.getAddressLine(i)).append("\n");
                sb.append(address.getLocality()).append("\n");
                sb.append(address.getPostalCode()).append("\n");
                sb.append(address.getCountryName());
        }
        String Address=sb.toString());
    }catch(Exception E);
    

    修复 UI 阻塞的解决方案

    使用 Thread 和 Handler 获取 Geocoder 答案而不阻塞 UI 的完整示例代码。

    Geocoder调用过程,可以位于Helper类中

    public static void getAddressFromLocation(
            final Location location, final Context context, final Handler handler) {
        Thread thread = new Thread() {
            @Override public void run() {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());   
                String result = null;
                try {
                    List<Address> list = geocoder.getFromLocation(
                            location.getLatitude(), location.getLongitude(), 1);
                    if (list != null && list.size() > 0) {
                        Address address = list.get(0);
                        // sending back first address line and locality
                        result = address.getAddressLine(0) + ", " + address.getLocality();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Impossible to connect to Geocoder", e);
                } finally {
                    Message msg = Message.obtain();
                    msg.setTarget(handler);
                    if (result != null) {
                        msg.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putString("address", result);
                        msg.setData(bundle);
                    } else 
                        msg.what = 0;
                    msg.sendToTarget();
                }
            }
        };
        thread.start();
    }
    

    这是在您的 UI Activity/Fragment 中对该 Geocoder 过程的调用:

    getAddressFromLocation(PinPointLocation, mContext, new GeocoderHandler());
    

    Activity/Fragment 中的处理程序类在您的 UI 中显示结果:

    private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            String result;
            switch (message.what) {
            case 1:
                Bundle bundle = message.getData();
                result = bundle.getString("address");
                break;
            default:
                result = null;
            }
            // replace by what you need to do
            myLabel.setText(result);
        }   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多