如何使用Google Place API从指定的纬度和经度获取地点地址

时间:2018-03-22 04:19:02

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

我正在制作像优步这样的出租车预订应用 用户拖动地图以使用引脚选择他的位置

我抓住那个引脚的LatLng。

enter image description here

这是我的代码:

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

如果应用程序在该位置运行,我希望获取该引脚的地址以向我的用户显示(例如优步)。

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

1 个答案:

答案 0 :(得分:2)

使用反向地理编码。首先从引脚点获取纬度和经度。更好地在后台线程中处理这个问题。否则会阻止用户界面。

describe

修复UI阻止的解决方案

使用线程和处理程序的完整示例代码,可在不阻止UI的情况下获取public sessionId; constructor(){ var socket = io.connect('http://**.***.***.***:****'); socket.on('connect', function (data) { console.log('user connected'); socket.emit('add user', { userId: uid }); }); socket.on('create channel', function (data) { console.log(data); this.sessionId = data.sessionId; //error this.function(); //error socket.emit('feedback', { received: true }); }); } function(){} 答案。

Geocoder调用程序,可以位于Helper类

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

以下是您在用户界面Geocoder中对此地理编码程序的调用:

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

Activity/Fragment中的处理程序类在您的用户界面中显示结果:

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