2012-04-10 10 views
0

irgendwelche Ideen, warum dies dazu führt, dass meine App schlecht verhalten? Ich habe in den Protokollen festgestellt, dass "Update vom GPS anfordern" und "Update vom Netzwerk anfordern" zweimal angezeigt wird. Wie auch immer, es ist mein Code. Jede Hilfe wird geschätzt.Google Maps und GeoCoding Verlangsamung App

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; 
+0

Das sieht so aus, als ob Sie 'Geocoder' aus dem UI-Thread verwenden - tun Sie das nicht, es wird Ihre Benutzeroberfläche träge machen und könnte Ihre App erzwingen. Verwenden Sie stattdessen eine 'AsyncTask' und führen Sie die Geocodierung in ihrer Methode' doInBackground() 'durch und aktualisieren Sie dann Ihre Benutzeroberfläche in' onPostExecute() '. –

+0

Verstanden! Danke Phil! –

+0

hat es sicher geholfen. So ein blinder Fehler. Vielen Dank! –

Antwort

1

Das sieht aus wie Sie Geocoder aus dem UI-Thread verwenden - tun Sie das nicht tun, wird es Ihre UI träge machen und könnte Ihre Anwendung zwangs schließen.

Verwenden Sie stattdessen eine AsyncTask und führen Sie die Geocodierung in seiner doInBackground() Methode, dann aktualisieren Sie Ihre Benutzeroberfläche in onPostExecute().