G. Joe
G. Joe

Reputation: 53

Google maps V2 Location returning null

I'm getting a null pointer exception when calling location.getLatitude in the initCamera method. I've tried all the solutions on stack but none have worked. Below are my fragment code, manifest and dependencies I'm using:

Java code

public class MapFragment extends SupportMapFragment implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        GoogleMap.OnInfoWindowClickListener,
        GoogleMap.OnMapLongClickListener,
        GoogleMap.OnMapClickListener,
        GoogleMap.OnMarkerClickListener {
    private GoogleApiClient mGoogleApiClient;
    private Location mCurrentLocation;
    private LocationRequest mLocationRequest;

    private final int[] MAP_TYPES = { GoogleMap.MAP_TYPE_SATELLITE,
            GoogleMap.MAP_TYPE_NORMAL,
            GoogleMap.MAP_TYPE_HYBRID,
            GoogleMap.MAP_TYPE_TERRAIN,
            GoogleMap.MAP_TYPE_NONE };
    private int curMapTypeIndex = 0;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHasOptionsMenu(true);

        mGoogleApiClient = new GoogleApiClient.Builder( getActivity() )
                .addConnectionCallbacks( this )
                .addOnConnectionFailedListener( this )
                .addApi( LocationServices.API )
                .build();


        initListeners();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient );
        Log.d("mCurrentLocation",mCurrentLocation +"");
        initCamera( mCurrentLocation );

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        removeListeners();
    }

    private void initListeners() {
        getMap().setOnMarkerClickListener(this);
        getMap().setOnMapLongClickListener(this);
        getMap().setOnInfoWindowClickListener( this );
        getMap().setOnMapClickListener(this);
    }

    private void removeListeners() {
        if( getMap() != null ) {
            getMap().setOnMarkerClickListener( null );
            getMap().setOnMapLongClickListener(null);
            getMap().setOnInfoWindowClickListener(null);
            getMap().setOnMapClickListener(null);
        }
    }
    private void initCamera( Location location ) {
        if (location != null){
            CameraPosition position = CameraPosition.builder()
                    .target( new LatLng( location.getLatitude(), location.getLongitude() ) )
                    .zoom( 16f )
                    .bearing( 0.0f )
                    .tilt( 0.0f )
                    .build();

            getMap().animateCamera( CameraUpdateFactory.newCameraPosition( position ), null );

            getMap().setMapType( MAP_TYPES[curMapTypeIndex] );
            getMap().setTrafficEnabled( true );
            getMap().setMyLocationEnabled( true );
            getMap().getUiSettings().setZoomControlsEnabled( true );
        } else {
            Log.d("hello","hello");
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        super.onStop();
        if( mGoogleApiClient != null && mGoogleApiClient.isConnected() ) {
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        //handle play services disconnecting if location is being constantly used
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        //Create a default location if the Google API Client fails. Placing location at Googleplex
        mCurrentLocation = new Location( "" );
        mCurrentLocation.setLatitude( 37.422535 );
        mCurrentLocation.setLongitude( -122.084804 );
        initCamera(mCurrentLocation);
    }


}

Manifest permission

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Dependencies

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.google.android.gms:play-services-maps:7.8.0'
    compile 'com.google.android.gms:play-services-location:7.8.0'
}

Upvotes: 0

Views: 129

Answers (1)

AndroidHacker
AndroidHacker

Reputation: 3596

It is a bug!!

You may find null values while fetching location data.

Firstly we need to make use of GoogleApiClient class.

Secondly we need to implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

Third we need to override its onConnected() , onConnectionSuspended() and onConnectionFailed() method.

Beside above mentioned you need to make use of onLocationChanged()

You will get location data when ever it is available in onLocationChanged().

Hope it clarifies.

Upvotes: 0

Related Questions