2015-11-21 68 views
0

目前我的android應用程序正在努力獲得gps座標並將它們打印到我的屏幕上。問題是他們有時似乎有300米的不準確。我不明白如何改變它,以便它實際上可以獲得GPS定位,並通過精確的座標不斷更新我的經度和緯度。Android GPS應用程序 - 如何更改它以持續更新GPS座標?

以下代碼是我的GPS處理程序。

public class GPSTracker extends Service implements LocationListener { 
 

 
    private final Context context; 
 

 
    boolean isGPSEnabled = false; 
 
    boolean isNetworkEnabled = false; 
 
    boolean canGetLocation = false; 
 

 
    Location location; 
 

 
    double latitude; 
 
    double longitude; 
 

 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; 
 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 1; 
 

 
    protected LocationManager locationManager; 
 

 
    public GPSTracker(Context context) { 
 
     this.context = context; 
 
     getLocation(); 
 
    } 
 

 
    public Location getLocation() { 
 
     try { 
 
      locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE); 
 

 
      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
 

 
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
 

 
      if (!isGPSEnabled && !isNetworkEnabled) { 
 

 
      } else { 
 
       this.canGetLocation = true; 
 

 
       if (isNetworkEnabled) { 
 

 
        locationManager.requestLocationUpdates(
 
          LocationManager.NETWORK_PROVIDER, 
 
          MIN_TIME_BW_UPDATES, 
 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
 

 
        if (locationManager != null) { 
 
         location = locationManager 
 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
 

 
         if (location != null) { 
 

 
          latitude = location.getLatitude(); 
 
          longitude = location.getLongitude(); 
 
         } 
 
        } 
 

 
       } 
 

 
       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(); 
 
          } 
 
         } 
 
        } 
 
       } 
 
      } 
 

 
     } catch (Exception e) { 
 
      e.printStackTrace(); 
 
     } 
 

 
     return location; 
 
    } 
 

 

 
    public void stopUsingGPS() { 
 
     if (locationManager != null) { 
 
      locationManager.removeUpdates(GPSTracker.this); 
 
     } 
 
    } 
 

 
    public double getLatitude() { 
 
     if (location != null) { 
 
      latitude = location.getLatitude(); 
 
     } 
 
     return latitude; 
 
    } 
 

 
    public double getLongitude() { 
 
     if (location != null) { 
 
      longitude = location.getLongitude(); 
 
     } 
 

 
     return longitude; 
 
    } 
 

 
    public boolean canGetLocation() { 
 
     return this.canGetLocation; 
 
    } 
 

 
    public void showSettingsAlert() { 
 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
 

 
     alertDialog.setTitle("GPS is settings"); 
 

 
     alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 
 

 
     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
 

 
      @Override 
 
      public void onClick(DialogInterface dialog, int which) { 
 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
 
       context.startActivity(intent); 
 
      } 
 
     }); 
 

 
     alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
 

 
      @Override 
 
      public void onClick(DialogInterface dialog, int which) { 
 
       dialog.cancel(); 
 
      } 
 
     }); 
 

 
     alertDialog.show(); 
 
    } 
 

 
    @Override 
 
    public void onLocationChanged(Location arg0) { 
 
     // TODO Auto-generated method stub 
 

 
    } 
 

 
    @Override 
 
    public void onProviderDisabled(String arg0) { 
 
     // TODO Auto-generated method stub 
 

 
    } 
 

 
    @Override 
 
    public void onProviderEnabled(String arg0) { 
 
     // TODO Auto-generated method stub 
 

 
    } 
 

 
    @Override 
 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
 
     // TODO Auto-generated method stub 
 

 
    } 
 

 
    @Override 
 
    public IBinder onBind(Intent intent) { 
 
     // TODO Auto-generated method stub 
 
     return null; 
 
    } 
 
}
 gps = new GPSTracker(MainActivity.this); 
 
     if(gps.canGetLocation()) { 
 
      latitude = gps.getLatitude(); 
 
      longitude = gps.getLongitude(); 
 
     } 
 
     else { 
 
      gps.showSettingsAlert(); 
 
     }

回答

0
Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_HIGH); 
locationManager.getBestProvider(criteria, true); 

希望這WIL幫助你。

+0

嗨,感謝您的幫助,你能告訴我:如果我使用gps = new GPSTracker(MainActivity.this); gps.getLatitude();我如何要求該座標的準確性? – Whiss

+0

也許[這](http://developer.android.com/reference/android/location/Location.html#getAccuracy())將幫助你 –

+0

如果我的答案對你有幫助,請upvote我的答案。 –

0

它看起來像您使用網絡提供商和GPS提供商獲取位置更新。由於網絡提供商依賴蜂窩塔和Wi-Fi信號,因此它不如GPS提供商準確。除去這些線從網絡提供商http://developer.android.com/guide/topics/location/strategies.html

只是禁用更新:

爲了解決這個問題,你可以通過這種方法

private static final int TWO_MINUTES = 1000 * 60 * 2; 

/** Determines whether one Location reading is better than the current Location fix 
    * @param location The new Location that you want to evaluate 
    * @param currentBestLocation The current Location fix, to which you want to compare the new one 
    */ 
protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     // A new location is always better than no location 
     return true; 
    } 

    // Check whether the new location fix is newer or older 
    long timeDelta = location.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    // If it's been more than two minutes since the current location, use the new location 
    // because the user has likely moved 
    if (isSignificantlyNewer) { 
     return true; 
    // If the new location is more than two minutes older, it must be worse 
    } else if (isSignificantlyOlder) { 
     return false; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(location.getProvider(), 
      currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and accuracy 
    if (isMoreAccurate) { 
     return true; 
    } else if (isNewer && !isLessAccurate) { 
     return true; 
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
     return true; 
    } 
    return false; 
} 

/** Checks whether two providers are the same */ 
private boolean isSameProvider(String provider1, String provider2) { 
    if (provider1 == null) { 
     return provider2 == null; 
    } 
    return provider1.equals(provider2); 
} 

來自過濾通過的LocationManager接收到的位置從您的代碼

if (isNetworkEnabled) { 

        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

         if (location != null) { 

          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 

       } 
+0

我試圖使用您建議的頂端方法來過濾位置,但我不知道如何請求我收到的要點的準確性。我知道該怎麼做才能得到一個點: latitude = gps.getLatitude(); longitude = gps.getLongitude(); – Whiss

+0

你可以在GPSTracker類 –

+0

中創建位置變量的getter,你能夠爲我提供寫作嗎?這是我第一次嘗試使用GPS功能,所以我有一些困難! – Whiss