0

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?

touchingtwist
  • 1,930
  • 4
  • 23
  • 38
  • 2
    Take a look at th answer here: http://stackoverflow.com/questions/34582370/how-can-i-use-google-maps-and-locationmanager-to-show-current-location-on-androi – Daniel Nugent Sep 21 '16 at 07:10
  • 1
    this might help you get your last location: https://developer.android.com/training/location/retrieve-current.html – user1506104 Sep 21 '16 at 07:14

1 Answers1

1

To get the last location, from the docs:

public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks, OnConnectionFailedListener {
    ...
    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }
}

Hope it helps!

jos
  • 1,070
  • 12
  • 22