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

时间:2022-01-13 03:53:53

I'm using a MyLocationOverlay to update the map with the users location as they're driving. 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. I'm not sure if the text .setText method is causing it to freeze or if the number of times the method gets called. 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在用户驾驶时更新地图。我正在尝试实现一个文本视图,根据街道名称,城市和州显示其当前位置。这一切都很好,但似乎MyLocationOverlay的更新频率导致地图滞后并冻结一两秒。我不确定文本.setText方法是否导致它冻结或者方法被调用的次数。使用名称city和state实现更新用户的正确方法是什么?我正在使用新线程这是正确的方法吗?这是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) 
                {

                }
            }
        }
    });
}

1 个解决方案

#1


0  

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.

您正在制作的地理编码器呼叫很可能是您计划中的瓶颈。我会减慢您对地理编码器的请求,看看您是否体验到了性能提升。您可能会失去一些粒度,但您的应用程序可能会更具响应性。

#1


0  

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.

您正在制作的地理编码器呼叫很可能是您计划中的瓶颈。我会减慢您对地理编码器的请求,看看您是否体验到了性能提升。您可能会失去一些粒度,但您的应用程序可能会更具响应性。