2012-04-10 3 views
0

내 앱이 제대로 동작하지 않는 이유가 무엇인가요? 로그에서 "GPS에서 업데이트 요청"및 "네트워크에서 업데이트 요청"이라는 메시지가 두 번 나타납니다. 어쨌든, 내 코드를 heres. 어떤 도움을 주시면 감사하겠습니다. 당신은 UI 스레드에서 Geocoder를 사용하는 것처럼Google지도 및 지오 코딩 속도 저하 앱

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

UI 스레드에서 '지오 코더 (Geocoder)'를 사용하는 것처럼 보입니다. 그렇게하지 않으면 UI가 느려지고 앱을 강제 종료 할 수 있습니다. 대신 'AsyncTask'를 사용하고'doInBackground()'메소드에서 지오 코딩을 한 다음'onPostExecute()'에서 UI를 업데이트하십시오. –

+0

알았습니다! 감사합니다 필! –

+0

확실히 도움이되었습니다. 그런 눈먼 실수. 감사! –

답변

1

이 보인다 - 그러지 마, 그것은 당신의 UI가 부진 할 것입니다 귀하의 응용 프로그램을 강제로 닫습니다 수 있습니다.

대신 AsyncTask을 사용하고 doInBackground() 방법으로 지오 코딩을 수행 한 다음 onPostExecute()에서 UI를 업데이트하십시오.