谷歌地图V2位置返回null

时间:2017-07-05 09:26:25

标签: android google-maps

我在initCamera方法中调用location.getLatitude时遇到空指针异常。我已经在堆栈上尝试了所有解决方案,但没有一个有效。下面是我正在使用的片段代码,清单和依赖项:

  

Java代码

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);
    }


}
  

清单许可

<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 {
    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'
}

1 个答案:

答案 0 :(得分:0)

这是一个错误!!

您在获取位置数据时可能会找到null值。

首先,我们需要使用GoogleApiClient类。

其次我们需要实现GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener

第三,我们需要覆盖它的onConnected(),onConnectionSuspended()和onConnectionFailed()方法。

除了上面提到的,你还需要使用onLocationChanged()

您将在 onLocationChanged()中获取位置数据。

希望它澄清。

相关问题