使用Geocoder根据纬度和经度检索地址会导致滞后。安卓

雷汉·尤萨夫(Rehan Yousaf)

我有一个地图活动,它的顶部还有一个EditText,并且我想在地图上有拖曳时在该EditText中显示更改的地址。我目前正在使用Geocoder类,以根据地图提供的坐标检索地址。但是这种方式在流畅的地图拖动动作中造成了很大的滞后。有没有更好的方法来检索地址,或者有更有效的方法来使用Geocoder类?现在,我已经在地图的OnCameraChangeListener事件中实现了getAddress()方法。这是代码。

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                double latitude=mMap.getCameraPosition().target.latitude;
                double longitude=mMap.getCameraPosition().target.longitude;
                String _Location="";
                Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                try {
                    List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
                    if(null!=listAddresses&&listAddresses.size()>0){
                        _Location = listAddresses.get(0).getAddressLine(0);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                EditText addressTxt=(EditText) findViewById(R.id.search_address_txt);
                addressTxt.setText(_Location);
            }
        });

我看到另一个应用程序实现了,其中搜索的地址位于地图拖动的末尾,并且地图的运动没有滞后。

你好,世界

Geocoder$getFromLocation()是阻塞调用,直到手段的API返回的地址将冻结UI线程。唯一的解决方案是在后台线程上运行此调用。

有两种方法可以实现此目的:

  1. 使用Google建议的方式-使用Intent Service您可以在此处找到有关如何操作的详细信息就我个人而言,我认为这过于矫kill而且非常复杂。
  2. 使用AsyncTask请参阅此内容以获取总体思路并根据您的需求实施。

此外,如果您使用的是Rx,则可以从地址解析器启动一个新的可观察对象,并在其他线程上进行观察,然后在主线程上进行订阅。

更新:我从一个旧项目中提取了此代码,看看是否有帮助。

AsyncGeocoderObject.java // object to pass to asynctask

public class AsyncGeocoderObject {

    public Location location; // location to get address from
    Geocoder geocoder; // the geocoder
    TextView textView; // textview to update text

    public AsyncGeocoderObject(Geocoder geocoder, Location location, TextView textView) {
        this.geocoder = geocoder;
        this.location = location;
        this.textView = textView;
    }
}


AsyncTask的实现

public class AsyncGeocoder extends AsyncTask<AsyncGeocoderObject, Void, List<Address>> {

private TextView textView;

@Override
protected List<Address> doInBackground(AsyncGeocoderObject... asyncGeocoderObjects) {
    List<Address> addresses = null;
    AsyncGeocoderObject asyncGeocoderObject = asyncGeocoderObjects[0];
    textView = asyncGeocoderObject.textView;
    try {
        addresses = asyncGeocoderObject.geocoder.getFromLocation(asyncGeocoderObject.location.getLatitude(),
                asyncGeocoderObject.location.getLongitude(), 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return addresses;
}

@Override
protected void onPostExecute(List<Address> addresses) {
    Log.v("onPostExecute", "location: " + addresses);
    String address;
    if (addresses != null)
        address = addresses.get(0).getLocality() + ", " + addresses.get(0).getCountryName();
    else address = "Service unavailable.";
    textView.setText(address);
}
}

位置更改时调用方法:

new AsyncGeocoder().execute(new AsyncGeocoderObject(
                new Geocoder(this),
                location,
                locationTV
        ));

希望这可以帮助!

更新2:
有关如何实现上述说明:

  1. 创建新类AsyncGeocoderObject并复制内容
  2. 创建另一个类(可以是活动中的子类,也可以是其他java文件中的子类),AsyncGeocoder然后复制内容。
  3. 现在,说您想获得用户的位置MainActivity并在textview中显示地址,例如locationTV实例化textview后,创建的新对象AsyncGeocoder并传递参数。例如:

    // in onCreate()
    new AsyncGeocoder().execute(new AsyncGeocoderObject(
            new Geocoder(this), // the geocoder object to get address
            location, // location object, 
            locationTV // the textview to set address on
    )); 
    

    另外,如果您没有位置对象,请使用所拥有的经度和纬度进行创建。有关如何操作,请参见这篇文章。

希望这对您有所帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用iOS中的Geocoder类根据地址获取经度和纬度

GeoCoder使用地址获取经度和纬度

当我知道使用Leaflet-Control-geocoder在React-Leaflet中的经度和纬度时,如何检索地点的地址?

使用Geocoder Gem验证经度/纬度

如何使用maxmind从IP地址获取经度和纬度?

如何使用Google API从onConnected()方法检索经度和纬度

根据Flutter中当前位置的经度和纬度获取完整的地址详细信息

根据地址或 xy 坐标创建地图 - 转换为纬度和经度?

如何根据纬度和经度距离从Sqlite数据库中检索行?

Geocoder寻找纬度和经度字段,但不存在

根据经度和纬度设置ggmap边界

根据起点按经度和纬度排序

打开安卓应用链接会导致安卓系统崩溃

如何检查特定地址的经度和纬度是否属于城市的纬度和经度?

如何从具有纬度和经度的mysql地址表中获取纬度和经度的距离

通过Google Maps API使用地址而不是经度和纬度

如何使用从服务中检索到的纬度和经度并将它们显示在地图上?

使用Xamarin表单的经度和纬度

使用python提取经度和纬度

使用经度和纬度获取rasterio的值

使用由纬度和经度构成的ID

使用Gps获取经度和纬度

从Android中的地址获取经度和纬度

如何从Google Map中的经度和纬度获取地址位置?

如何从地址或街道获取经度和纬度?

从地址地理位置获取纬度和经度

开放层地图,经度和纬度获取地址

如何从抓取谷歌地图的地址获取纬度和经度?

在Asp.net中获取地址的经度和纬度?