2012-01-13 89 views
1

獲取經緯度,lon值爲一或兩個建築物的距離,並沒有得到確切的緯度和經度值。我試圖獲得至少一兩米精度爲當前位置的緯度和經度值。這是代碼如何獲得一兩米精度的經緯度值爲當前位置

public class GPSLocatorActivity extends MapActivity implements OnClickListener { 
MapView mapView = null; 
MapController mapController = null; 
MyLocationOverlay whereAmI = null; 
private Button bdiffaddr, bnext; 
MyLocation myLocation; 
GeoPoint p = null; 
MapController mc = null; 
public static LocationManager locManager; 
protected ProgressDialog progressDialog; 
public static double latitude, longitude; 
public String TAG = "GPSLocatorActivity"; 

protected boolean isLocationDisplayed() { 
    return whereAmI.isMyLocationEnabled(); 
} 
protected boolean isRouteDisplayed() { 
    return false; 
} 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gpslocation); 
    initialiseviews(); 
    // onclick listeners 
    onclicklisteners(); 
    currentLocLatLong(); 
    // new GoogleMapAsyncTask().execute(); 
    GPSLocatorActivity.this.runOnUiThread(new Runnable() { 
     public void run() { 
      mapView = (MapView) findViewById(R.id.mapView); 
      mapView.setBuiltInZoomControls(true); 
      mapController = mapView.getController(); 
      mapController.setZoom(15); 
      whereAmI = new MyLocationOverlay(GPSLocatorActivity.this, 
        mapView); 
      mapView.getOverlays().add(whereAmI); 
      mapView.postInvalidate(); 
     } 
    }); 

} 
public class GoogleMapAsyncTask extends AsyncTask<Void, Void, Void> { 
    protected Void doInBackground(Void... voids) { 
     mapView = (MapView) findViewById(R.id.mapView); 
     mapView.setBuiltInZoomControls(true); 
     mapController = mapView.getController(); 
     mapController.setZoom(15); 
     whereAmI = new MyLocationOverlay(GPSLocatorActivity.this, mapView); 
     mapView.getOverlays().add(whereAmI); 
     mapView.postInvalidate(); 
     return null; 
    } 

} 
private void currentLocLatLong() { 
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100L, 
      100.0f, locationListener); 
    Location location = locManager 
      .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (location != null) { 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
    } 
} 
private void updateWithNewLocation(Location location) { 
     String latLongString = ""; 
    Log.d("Lat: + latitude + \nLong: + longitude", "Lat:" + latitude 
      + "\nLong:" + longitude); 
    if (location != null) { 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
     latLongString = "Lat:" + latitude + "\nLong:" + longitude; 
    } else { 
     latLongString = "No location found"; 
    } 
} 
public void onResume() { 
    super.onResume(); 
    whereAmI.enableMyLocation(); 
    whereAmI.runOnFirstFix(new Runnable() { 
     public void run() { 
      mapController.setCenter(whereAmI.getMyLocation()); 
     } 
    }); 

} 
public void onPause() { 
    super.onPause(); 
    whereAmI.disableMyLocation(); 
} 
public void onLocationChanged(Location location) { 
    if (location != null) { 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
     @SuppressWarnings("unused") 
     String currentLocation = "Lat: " + latitude + " Lng: " + longitude; 
     // txted.setText(currentLocation); 
     p = new GeoPoint((int) latitude * 1000000, 
       (int) longitude * 1000000); 
     mc.animateTo(p); 
    } 
} 
public final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     updateWithNewLocation(location); 
    } 
    public void onProviderDisabled(String provider) { 
     updateWithNewLocation(null); 
    } 
    public void onProviderEnabled(String provider) { 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
}; 

please help if anyone has idea how to get the accuracy upto one or two meter distance. 

回答

2

對不起,您無法通過編程來提高準確性。隨着接收更多的衛星或使用更好的GPS硬件或擴展系統(如航空中使用的WAAS),精度會提高。此外,建築物外部的接收效果更好,遠離衛星直接視線中的任何障礙物。

要確定你有多少顆衛星接收,你可以看看這裏:https://stackoverflow.com/a/8747795/1127492

BTW 2米是非常具有挑戰性的。有關準確度的更多信息,請參閱此處:http://www.gps.gov/systems/gps/performance/accuracy/#difference

相關問題