2012-01-06 84 views
0

我需要計算Gps在移動位置和偏離某個位置的位置。我嘗試過使用駕駛指示,但不贊成任何類似的事情會很好。如何計算移動GPS位置?

+0

意味着你要ÿ我們的位置在經度和緯度? – user1089679 2012-01-06 09:35:05

回答

0
LocationListener locationListener = new LocationListener() { 
       public void onLocationChanged(Location location) { 
       location.getLatitude(); 
          location.getLongitude() 
        // what you want to do 
       } 

       public void onStatusChanged(String provider, int status, 
         Bundle extras) { 
        Toast.makeText(LocationInfoActivity.this, 
          String.valueOf("onStatusChanged " + provider), 
          Toast.LENGTH_SHORT).show(); 
       } 

       public void onProviderEnabled(String provider) { 
        Toast.makeText(LocationInfoActivity.this, 
          String.valueOf("onProviderEnabled " + provider), 
          Toast.LENGTH_SHORT).show(); 
       } 

       public void onProviderDisabled(String provider) { 
        Toast.makeText(LocationInfoActivity.this, 
          String.valueOf("onProviderDisabled " + provider), 
          Toast.LENGTH_SHORT).show(); 
       } 
      }; 


      try { 
       locationManager.requestLocationUpdates(
         LocationManager.GPS_PROVIDER, 10000, 1, locationListener); 

    // the 1 denotes meter (deviation) 
      } catch (Exception e) { 
       Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 10000, 1, 
         locationListener); 
       locationManager.requestLocationUpdates(
         LocationManager.PASSIVE_PROVIDER, 10000, 1, 
         locationListener); 
      } 

     } 

參考: http://developer.android.com/guide/topics/location/obtaining-user-location.html

+1

您可以評論一下爲什麼使用String.valueOf(「onStatusChanged」+ provider)而不是僅僅使用onStatusChanged + provider?第二種方式不僅等同,而且更短,更快,IMO更具可讀性。 – 2012-01-06 10:17:10