John Nguyen
John Nguyen

Reputation: 557

Google Maps and GeoCoding Slowing Down App

any ideas why this is causing my app to behave poorly? I did notice in the logs that "Request updates from GPS" and "Request update from Network" does appear twice. Anyways, heres my code. Any help will be appreciated.

 public class statuspage extends MapActivity {

private MapController mapController;
private MyLocationOverlay myLocation;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.statuspage);

    // Get Mapping Controllers etc
    MapView mapView = (MapView) findViewById(R.id.mapView);
    mapController = mapView.getController();
    mapController.setZoom(14);
    mapView.setBuiltInZoomControls(true);

    // Add the MyLocationOverlay
    myLocation = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocation);
    myLocation.enableMyLocation();
    myLocation.runOnFirstFix(new Runnable() {

        public void run() {
            mapController.animateTo(myLocation.getMyLocation());

        }
    });
}

class MapOverlay extends com.google.android.maps.Overlay {
}

@Override
protected boolean isRouteDisplayed() {

    // Location Manager Intiation
    LocationManager locationManager;
    String bestProvider;
    String LOCATION_SERVICE = "location";
    locationManager = (LocationManager) this
            .getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    // More accurate, GPS fix.
    criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix.
    bestProvider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {

        // Latitude and Longitude TextView
        EditText etlongitude = (EditText) findViewById(R.id.etlongitude);
        EditText etlatitude = (EditText) findViewById(R.id.etlatitude);

        // Disables longitude textview Keyboard Popup.
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(etlongitude.getWindowToken(), 0);

        // Disables latitude textview Keyboard Popup.
        InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm2.hideSoftInputFromWindow(etlatitude.getWindowToken(), 0);


        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        // Latitude and Longitude TextView Display Coordinates
        etlongitude.setText("" + (longitude)); 
        /** longitude textview */
        etlatitude.setText("" + (latitude));
        /** latitude textview */

        String addressString = "No address found";

        Geocoder gc = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = gc.getFromLocation(latitude,
                    longitude, 1);
            StringBuilder sb = new StringBuilder();
            if (addresses.size() > 0) {
                Address address = addresses.get(0);

                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)


                sb.append(address.getAddressLine(i)).append("\n");
                //sb.append(address.getFeatureName()).append("\n");
                //sb.append(address.getPhone()).append("\n");
                //sb.append(address.getLocality()).append("\n");
                //sb.append(address.getPostalCode()).append("\n");
                //sb.append(address.getCountryName());
            }
            addressString = sb.toString();

            TextView scrollview = (TextView) findViewById(R.id.scrollview);
            scrollview.setText("Your location:" + "\n" + "(Accurate to 500 meters)" +"\n" +(addressString));

        } catch (IOException e) {
        }

        // locationManager.removeUpdates((LocationListener) location);
        locationManager = null;

Upvotes: 0

Views: 539

Answers (1)

Philipp Reichart
Philipp Reichart

Reputation: 20961

This looks like you're using Geocoder from the UI thread -- don't do that, it will make your UI sluggish and might force-close your app.

Instead, use an AsyncTask and do the geocoding in its doInBackground() method, then update your UI in onPostExecute().

Upvotes: 1

Related Questions