0

任何想法为什么这会导致我的应用程序表现不佳?我确实在日志中注意到“从 GPS 请求更新”和“从网络请求更新”确实出现了两次。无论如何,这是我的代码。任何帮助将不胜感激。

 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;
4

1 回答 1

1

这看起来像是您在GeocoderUI 线程中使用的——不要那样做,它会使您的 UI 变得迟缓并可能强制关闭您的应用程序。

AsyncTask相反,在其方法中使用and 进行地理编码doInBackground(),然后在onPostExecute().

于 2012-04-11T11:20:47.333 回答