Android Google Maps V2 当前位置 纬度 经度 NullPointerException
Posted
技术标签:
【中文标题】Android Google Maps V2 当前位置 纬度 经度 NullPointerException【英文标题】:Android Google Maps V2 current position latitude longitude NullPointerException 【发布时间】:2015-05-06 19:42:46 【问题描述】:有很多类似的问题,但我没有找到任何解决我的问题的方法。 setUpMap 方法是:
private void setUpMap()
BitmapDescriptor iconm = BitmapDescriptorFactory.fromResource(R.drawable.m);
BitmapDescriptor iconc = BitmapDescriptorFactory.fromResource(R.drawable.c);
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider;
provider = locationManager.getBestProvider(criteria,true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
boolean isNWEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isNWEnabled)
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getString(R.string.askposition) );
dialog.setPositiveButton(getString(R.string.settings), new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt)
// TODO Auto-generated method stub
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
);
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt)
// TODO Auto-generated method stub
);
dialog.show();
// no network provider is enabled
Log.e("Current Location", "Current Lat Lng is Null");
else
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
if (myLocation == null)
if (locationManager != null)
myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
//myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//double latitude = myLocation.getLatitude();
//double longitude = myLocation.getLongitude();
//String lati = String.valueOf(latitude);
//String longi = String.valueOf(longitude);
// Create a LatLng object for the current location
//LatLng latlng = new LatLng(latitude, longitude);
// set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();
//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// Show the current location in Google Map
// mMap.moveCamera(newLatLng(latlng));
// Zoom in the Google Map
//mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
else
// First get location from Network Provider
if (isNWEnabled)
if (myLocation == null)
if (locationManager != null)
myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
//double latitude = myLocation.getLatitude();
//double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
//LatLng latlng = new LatLng(latitude, longitude);
// set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Zoom in the Google Map
//CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();
//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
// Show the current location in Google Map
//mMap.moveCamera(newLatLng(latlng));
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(11).build();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
当我第一次将应用程序从 Android Studio 运行到我的设备时它工作正常,如果我关闭并重新启动应用程序也工作正常。
但是当我重新启动智能手机时,它给了我 NullPointerException @ double latitude = myLocation.getLatitude();
从那一刻起,它总是崩溃。
我该如何解决?
注释的代码行几乎没有尝试。完全一样的问题。
任何帮助将非常感激。
提前致谢。
【问题讨论】:
developer.android.com/reference/android/location/…。引用文档 如果提供程序当前被禁用,则返回 null。还要检查这个gabesechansoftware.com/location-tracking/#comments。更新:developer.android.com/training/location/… 你可以使用try catch来避免崩溃。把你的代码放在try catch中 只是一个建议。如果您在 Android Studio 中开发,您可以使用:github.com/mcharmas/Android-ReactiveLocation 获取最新的融合位置 什么时候重启手机 GPS 开启? @Raghunandan 我正在查找,谢谢。 Kat-hat 我会尝试,但我会知道为什么。 cYrixmorten 谢谢你的建议,我稍后再试,希望能用实际代码解决。 Manny264 是的。网络和 GPS 都有 【参考方案1】:我认为解决您的问题的方法是使用PASSIVE_PROVIDER。
示例代码:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
double longitude = myLocation.getLongitude();
double latitude = myLocation.getLatitude();
private final LocationListener locationListener = new LocationListener()
public void onLocationChanged(Location location)
longitude = location.getLongitude();
latitude = location.getLatitude();
【讨论】:
仅使用网络关闭 GPS 提供程序似乎可以工作。但是你的建议真的很有帮助!谢谢【参考方案2】:完整的解决方案,包括棉花糖及以上的权限检查
//get location manger instance
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
//check if we have permission to access device location
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
//add location change listener with update duration 2000 millicseconds or 10 meters
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new LocationListener()
public void onLocationChanged(Location location)
currentLongitude = location.getLongitude();
currentLatitude = location.getLatitude();
@Override
public void onStatusChanged(String s, int i, Bundle bundle)
@Override
public void onProviderEnabled(String s)
@Override
public void onProviderDisabled(String s)
);
//get last known location to start with
Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
currentLatitude = myLocation.getLatitude();
currentLongitude = myLocation.getLongitude();
【讨论】:
以上是关于Android Google Maps V2 当前位置 纬度 经度 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
Android- Google Maps V2:跟踪从当前位置到另一个目的地的路线
在 Android google maps api v2 for 4.0 中,显示从当前位置到给定纬度的路线
Android - Google maps V2 - 计算当前不在视野中的标记是屏幕的北、东、南还是西
Android : Google Maps API V2 显示黑屏 ECLIPSE