0
CODE:如何獲取googlemap上的最後位置?
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
Toast.makeText(getApplicationContext(), "oh, no", Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(), "test"+mLastLocation.getLatitude(), Toast.LENGTH_LONG).show(); #Here is what i want.
}
}
後,我通過mMap.setMyLocationEnabled(true);
讓我的位置,
我想要得到的mMap.setMyLocationEnabled(true);
結果的Latitude or Longitude
。
但上面的代碼返回
FATAL EXCEPTION: main
Process: com.example.keepair.myapplication_maptest, PID: 15483
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.example.keepair.myapplication_maptest.MapsActivity.onMapReady(MapsActivity.java:48)
問:有沒有什麼好的辦法或方法?
我不在乎你的回答是否說明如何得到mMap.setMyLocationEnabled(true);
的結果Latitude or Longitude
。
如果您的答案確實檢查最後的位置信息,那就沒問題。
如果您的答案檢查在谷歌地圖上最後clicked point's coordinates
或setMyLocation's coordinates
,我也會很高興。
請你讓我知道嗎?
請看這裏的答案:http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi –
這可能會幫助您獲得最後一個位置:https://developer.android.com/training/location/retrieve-current.html – user1506104