简体   繁体   中英

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. 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:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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