如何在android中跟踪GPS位置?

如何在android中跟踪GPS位置?,android,gps,Android,Gps,我有一个android应用程序,其中列出了gps位置。 如何获取用于创建此视图的gps位置?这可以使用位置管理器完成。是一个如何使用它的示例,也是它的API参考。我已经完成了这项工作 private void _getLocation() { // Get the location manager LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); C

我有一个android应用程序,其中列出了gps位置。
如何获取用于创建此视图的gps位置?

这可以使用位置管理器完成。是一个如何使用它的示例,也是它的API参考。

我已经完成了这项工作

   private void _getLocation()
  {
  // Get the location manager
LocationManager locationManager = (LocationManager)  getSystemService(LOCATION_SERVICE);
   Criteria criteria = new Criteria();
   String bestProvider = locationManager.getBestProvider(criteria, false);
  Location location = locationManager.getLastKnownLocation(bestProvider);

try {
lat = location.getLatitude ();
lon = location.getLongitude ();
}
catch (NullPointerException e){
 lat = -1.0;
 lon = -1.0;
 }
很简单。它将获得最佳可用的提供者,并获得其最后已知的位置。 如果您只想使用GPS,请从此处开始执行此操作。说明了如何很好地获取GPS坐标

打开AndroidManifest.xml并添加
ACCESS\u FINE\u LOCATION
(其中包括
ACCESS\u FINE\u LOCATION
ACCESS\u rough\u LOCATION
)。此外,如果您正在获取基于网络的位置,则还需要添加
互联网
权限

在本教程中:

可能重复的问题
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
    if (location == null) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES,
                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (locationManager != null) {
            location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }
    }