Java Android位置管理器返回空位置数据
我有一个很有趣的问题, 我的位置管理器有时工作,有时返回空数据 我检查所有权限、渐变文件选项和运行时位置检查是否启用。我的手机没有飞行模式,我也没有关闭位置 无论我检查所有数据是什么,获取位置数据都没有问题 主代码Java Android位置管理器返回空位置数据,java,android,android-maps-v2,android-location,Java,Android,Android Maps V2,Android Location,我有一个很有趣的问题, 我的位置管理器有时工作,有时返回空数据 我检查所有权限、渐变文件选项和运行时位置检查是否启用。我的手机没有飞行模式,我也没有关闭位置 无论我检查所有数据是什么,获取位置数据都没有问题 主代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
cs = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("verigeldi");
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return; // check permission
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,1000, mLocationListener);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try { // THIS AREA VERY IMPORTANT !!!!
longitude = location.getLongitude(); // GET NULL POINTER EXCEPTION SOMETIMES WORKING
latitude = location.getLatitude(); // GET NULL POINTER EXCEPTION SOME TIMES WORKING
LatLng latLng = new LatLng(latitude, longitude);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 13);
mMap.animateCamera(cameraUpdate);
}
catch (Exception e) {
LatLng latLng = new LatLng(39.052540, 35.410083);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 5);
mMap.animateCamera(cameraUpdate);
}
checkLocationIsEnabled();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
googleMap.setMyLocationEnabled(true);
}
public final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 13);
mMap.animateCamera(cameraUpdate);
mLocationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
cs=(ArrayList)getIntent().getSerializableExtra(“verigeldi”);
mLocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
location=mlLocationManager.getLastKnownLocation(LocationManager.GPS\U提供程序);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
return;//检查权限
}
mLocationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,10001000,mLocationListener);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
试试{//这个区域非常重要!!!!
longitude=location.getLongitude();//有时会出现获取空指针异常
latitude=location.getLatitude();//有时会出现空指针异常
LatLng LatLng=新LatLng(纬度、经度);
CameraUpdate CameraUpdate=CameraUpdateFactory.newLatLngZoom(latLng,13);
mMap.animateCamera(摄影机更新);
}
捕获(例外e){
LatLng LatLng=新LatLng(39.052540,35.410083);
CameraUpdate CameraUpdate=CameraUpdateFactory.newLatLngZoom(latLng,5);
mMap.animateCamera(摄影机更新);
}
checkLocationIsEnabled();
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
返回;
}
googleMap.setMyLocationEnabled(true);
}
公共最终位置Listener mLocationListener=新位置Listener(){
@凌驾
已更改位置上的公共无效(位置){
LatLng LatLng=新LatLng(location.getLatitude(),location.getLongitude());
CameraUpdate CameraUpdate=CameraUpdateFactory.newLatLngZoom(latLng,13);
mMap.animateCamera(摄影机更新);
mLocationManager.RemoveUpdate(此);
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
};
问了这么多次:,。。。只是提一些。下次先搜索!是的,搜索了这么多次,不使用此链接,好吗?您是否尝试将mLocationManager.requestLocationUpdates
中的1000毫秒更改为0,并移动行location=mLocationManager.getLastKnownLocation(LocationManager.GPS\u PROVIDER)代码>之后?所有链接都起作用,第一个链接试图用getLastKnownLocation()
更详细地解释问题。或者您的问题是在onLocationChanged()
中实际获得null
位置?这不应该发生,但我有时会看到它,所以我只是检查那里的位置
对象,如果它为空
,则忽略它,。。。只是提一些。下次先搜索!是的,搜索了这么多次,不使用此链接,好吗?您是否尝试将mLocationManager.requestLocationUpdates
中的1000毫秒更改为0,并移动行location=mLocationManager.getLastKnownLocation(LocationManager.GPS\u PROVIDER)代码>之后?所有链接都起作用,第一个链接试图用getLastKnownLocation()
更详细地解释问题。或者您的问题是在onLocationChanged()
中实际获得null
位置?这不应该发生,但我有时会看到它,所以我只是检查那里的位置
对象,如果为null
则忽略它。