2012-04-10 3 views
0

любые идеи, почему это заставляет мое приложение вести себя плохо? Я заметил в журналах, что «Запросить обновления из GPS» и «Запросить обновление из сети» появляется дважды. В любом случае, мой код. Любая помощь будет оценена.Google Maps и GeoCoding Slowing Down 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

Похоже, вы используете 'Geocoder' из потока пользовательского интерфейса - не делайте этого, это сделает ваш пользовательский интерфейс вялым и может заставить закрыть ваше приложение. Вместо этого используйте 'AsyncTask' и выполните геокодирование в методе' doInBackground() ', а затем обновите свой интерфейс в' onPostExecute() '. –

+0

Получил! Спасибо, Фил! –

+0

это действительно помогло. Такая слепая ошибка. благодаря! –

ответ

1

Это выглядит как вы используете Geocoder из потока пользовательского интерфейса - не делайте этого, это сделает ваш интерфейс вялым и может принудительно закрыть приложение.

Вместо этого используйте AsyncTask и выполните геокодирование в своем методе doInBackground(), а затем обновите свой интерфейс пользователя в onPostExecute().

Смежные вопросы