parker parker - 2 years ago 113
Android Question

how to get current location in google map android

Actually my problem is I am not getting current location latitude and longitude I tried so many ways.I know that this question already asked in SO I tried that answers also still I didn't get answer.Please help me

if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(;

// check if map is created successfully or not
if (googleMap == null) {
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
Location myLocation = googleMap.getMyLocation(); //Nullpointer exception.........
LatLng myLatLng = new LatLng(myLocation.getLatitude(),

CameraPosition myPosition = new CameraPosition.Builder()

Answer Source

please check the sample code of the Google Maps Android API v2 , Using this you will solve your problem.

Sample Code

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            // Check if we were successful in obtaining the map.
            if (mMap != null) {

             mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

           public void onMyLocationChange(Location arg0) {
            // TODO Auto-generated method stub

             mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));


call this function in on create function

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download