谷歌地图 V2 位置返回 null
Posted
技术标签:
【中文标题】谷歌地图 V2 位置返回 null【英文标题】:Google maps V2 Location returning null 【发布时间】:2017-07-05 09:26:24 【问题描述】:在 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'
【问题讨论】:
***.com/questions/25017764/… 该链接中没有解决方案 相信您还没有阅读我提供的完整解决方案。 【参考方案1】:这是一个错误!
在获取位置数据时,您可能会发现 null
值。
首先我们需要使用 GoogleApiClient 类。
其次我们需要实现GoogleApiClient.ConnectionCallbacks、GoogleApiClient.OnConnectionFailedListener
第三,我们需要重写它的 onConnected() 、 onConnectionSuspended() 和 onConnectionFailed() 方法。
除了上面提到的你还需要使用onLocationChanged()
您将在 onLocationChanged() 中获得位置数据。
希望它澄清。
【讨论】:
以上是关于谷歌地图 V2 位置返回 null的主要内容,如果未能解决你的问题,请参考以下文章