如何通过Google Place API获取指定经纬度的地点地址
How to get the place address from specified latitude and longitude with Google Place API
我正在制作像 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 的位置地址,以便向我的用户显示该应用程序是否在该位置运行(就像优步一样)。
如何从引脚位置坐标获取该地址?
使用反向地理编码。首先从针点获取纬度和经度。最好在后台线程中处理这个问题。否则会阻塞 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阻塞的解决方案
使用线程和处理程序获取 Geocoder
答案而不阻塞 UI 的完整示例代码。
Geocoder调用程序,可以定位到Helper中class
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
中的处理程序 class 以在您的 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);
}
}
我正在制作像 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 的位置地址,以便向我的用户显示该应用程序是否在该位置运行(就像优步一样)。
如何从引脚位置坐标获取该地址?
使用反向地理编码。首先从针点获取纬度和经度。最好在后台线程中处理这个问题。否则会阻塞 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阻塞的解决方案
使用线程和处理程序获取 Geocoder
答案而不阻塞 UI 的完整示例代码。
Geocoder调用程序,可以定位到Helper中class
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
:
getAddressFromLocation(PinPointLocation, mContext, new GeocoderHandler());
和 Activity/Fragment
中的处理程序 class 以在您的 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);
}
}