CODE :
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.
}
}
After i get my location through mMap.setMyLocationEnabled(true);
,
I want get the Latitude or Longitude
of the result of mMap.setMyLocationEnabled(true);
.
but above code returned
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)
Question : Is there any good way or method?
I don't care whether your answer says not exactly how to get the Latitude or Longitude
of the result of mMap.setMyLocationEnabled(true);
.
If your answer do check last location information, it's ok.
I'll be also happy if your answer check last clicked point's coordinates
or setMyLocation's coordinates
on googlemap.
Please would you let me know?