【问题标题】:How would I store and display a location on Google Maps using the address entered by the user and not the lat or long?如何使用用户输入的地址而不是纬度或经度在 Google 地图上存储和显示位置?
【发布时间】:2017-04-15 16:46:48
【问题描述】:

我正在使用 Android Studio 为作业制作应用程序,当用户刚刚输入他们的街道地址、城市、邮政编码时,我将如何在 Google 地图上存储和显示位置并在注销后留在那里/zip code , country 但不是纬度或经度坐标?

【问题讨论】:

    标签: java android google-maps geolocation


    【解决方案1】:

    首先,您需要将地址转换为匹配的经纬度对。 因此,如果我输入Amsterdam,它将返回该城市的纬度和对数。 这种转换称为地理编码。有一个Android tutorial 可以使用Geocoder class 来做这件事。 如果您只想简单地使用第一个值,这里是 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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2012-10-09
      • 1970-01-01
      • 2013-04-02
      • 2013-02-26
      • 2016-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多