简体   繁体   English

Android:在MyLocationOverlay中进行反向地理编码的最佳方法?

[英]Android: Best way to do reverse geocoding in MyLocationOverlay?

I'm using a MyLocationOverlay to update the map with the users location as they're driving. 我正在使用MyLocationOverlay来更新用户驾驶时的位置地图。 I'm trying to implement a text view that shows their current location in terms of the street name, city and state. 我正在尝试实现一个文本视图,以街道名称,城市和州来显示其当前位置。 This all works fine but it seems like the update frequency of MyLocationOverlay is causing the map to lag and freeze for a second or two. 一切正常,但MyLocationOverlay的更新频率似乎导致地图滞后并冻结了一两秒钟。 I'm not sure if the text .setText method is causing it to freeze or if the number of times the method gets called. 我不确定文本.setText方法是否导致其冻结或该方法被调用的次数。 What is the proper way to implement updating the user with the name city and state? 用城市和州名更新用户的正确方法是什么? I'm using a new thread is this the right way? 我正在使用新线程,这是正确的方法吗? Here's my code in the onLocationChanged method of MyLocationOverlay: 这是MyLocationOverlay的onLocationChanged方法中的代码:

@Override
public synchronized void onLocationChanged(Location location) {
    super.onLocationChanged(location);
    mLocation = location;
    // only move to new position if enabled and we are in an border-area
    if (this.isMyLocationEnabled() && animateToCurrentLocation) {
        mMapController.animateTo(getMyLocation());
    }

    this.runOnFirstFix(new Runnable() {
        public void run() {
            Log.d(TAG, "Running");
            if (mLocation != null) {
                Geocoder gc = new Geocoder(mContext, Locale.getDefault());

                try 
                {
                    List<Address> addresses = gc.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1);
                    if (addresses != null && addresses.size() > 0) 
                    {
                        txtStreetAddress.setText(addresses.get(0).getThoroughfare() + " " + addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea());
                    }
                } catch (IOException e) 
                {

                }
            }
        }
    });
}

It is likely that the geocoder calls you are making are the bottleneck in your program. 您正在执行的地址解析器调用很可能是程序的瓶颈。 I would slow down your requests to the geocoder and see if you experience improved performance. 我会放慢您对地址解析器的请求,看看您的性能是否有所提高。 You may lose a bit of granularity but your application will probably be more responsive. 您可能会失去一些粒度,但是您的应用程序可能会响应更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM