[英]How would I store and display a location on Google Maps using the address entered by the user and not the lat or long?
我正在使用Android Studio制作用于分配任务的应用程序,如何在用户仅输入街道地址,城市,邮政编码/邮政编码后注销后将位置存储并显示在Google地图上并留在那里,国家/地区,而不是经纬度或长期坐标?
First you need to convert the address to a matching latitude longitude pair. 首先,您需要将地址转换为匹配的纬度经度对。 So if I would enter Amsterdam
, it would return the latitude and logitude of that city. 因此,如果我输入Amsterdam
,它将返回该城市的纬度和经度。 This conversion is called geocoding. 这种转换称为地理编码。 There's an Android tutorial for doing exactly this using the Geocoder class . 有一个Android教程 ,可以使用Geocoder类精确地做到这一点。 If you want to just simple use the first value, here's an example with RxJava 2: 如果只想简单地使用第一个值,这是RxJava 2的示例:
private Geocoder _geocoder;
public Observable<Location> convertAddressToLatLng(final String address) {
if (_geocoder == null) {
_geocoder = new Geocoder(_context);
}
return Observable.just(address)
.map(new Func1<String, Location>() {
@Override
public Location call(String s) {
try {
List<Address> addressList = _geocoder.getFromLocationName(address, 1);
if (addressList != null && addressList.size() > 0) {
Address bestMatch = addressList.get(0);
Location result = new Location("");
result.setLatitude(bestMatch.getLatitude());
result.setLongitude(bestMatch.getLongitude());
return result;
} else {
return null;
}
} catch (IOException ex) {
Log.e(TAG, "Error while geocoding!", ex);
return null;
}
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.