2016-11-18 57 views
-2

我想獲得設備的確切座標。如何獲取Android開發中的精確經度和緯度?

正如你在我的代碼中看到的,我嘗試過使用位置管理器,但它只指向我所在的國家,我需要確切的位置。

@Override 
public void onMapReady(GoogleMap googleMap) { 
    LocationManager lm = (LocationManager)getSystemService(this.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    double longitude = location.getLongitude(); 
    double latitude = location.getLatitude(); 
    mMap = googleMap; 


    LatLng myMarker = new LatLng(longitude,latitude); 

    mMap.addMarker(new MarkerOptions().position(myMarker).title("I am here")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myMarker)); 
} 

有沒有更好的方法可以做到這一點,請幫助嗎?

+2

閱讀本https://developer.android.com/training/location/index.html和你之前搜索問 –

+0

好的,謝謝... – user3576490

回答

1

你應該要求位置更新,這將給你的位置,更高的精度,是這樣的:

Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_HIGH); 
     criteria.setPowerRequirement(Criteria.POWER_MEDIUM); 

     locationManager.requestLocationUpdates(5000, 0, criteria, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       double longitude = location.getLongitude(); 
       double latitude = location.getLatitude(); 
       // Here you have precise location 
       locationManager.removeUpdates(this); 
      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 

      } 
     }, null);