位置正在返回 null LocationManager.getlastlocation Gps 提供商
Location is Returning null LocationManager.getlastlocation Gps Provider
我已经搜索并实施了各种方法来获取我的位置。但无论我做什么,位置始终为空。它是空的。我的应用程序几乎完成了,这是唯一在这里出现问题的是我的代码。
public Location getlocation() {
Location loc = null;
LocationManager lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
return loc;
}
@Override
public void onMapReady(GoogleMap googleMap) {
setmap(googleMap);
}
public void setmap(final GoogleMap map) {
googlemap = map;
if (googlemap != null) {
if (sharedPreferences.getBoolean(CustomAlarm.satellite, false)) {
googlemap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
if(getlocation() == null){
Toast.makeText(getContext(),"You don't live on this earth or Location null",Toast.LENGTH_LONG).show();
}
评论前请注意这一点。
1- 我正在调试我的手机 phone samsung j1 api 19 级。
2- 已启用位置和 gps。
3- 尝试在 googlemaps 中定位我的位置。
4- 还启用了 Google 位置记录。
但是没有任何帮助。
曾经遇到过同样的问题。
我通过请求位置更新解决了这个问题。
Google recommendations about it
一段时间后,位置被捕获。
您正在请求您在这里面的位置
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
这意味着如果未授予权限。
行 loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
在授予 IS 权限(可能是)时永远不会执行,因此将保持与您之前分配的值相同 Location loc = null;
所以 null.
您应该将 if 语句更改为
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
这意味着如果位置权限 IS 授予然后给我位置。
我已经搜索并实施了各种方法来获取我的位置。但无论我做什么,位置始终为空。它是空的。我的应用程序几乎完成了,这是唯一在这里出现问题的是我的代码。
public Location getlocation() {
Location loc = null;
LocationManager lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
return loc;
}
@Override
public void onMapReady(GoogleMap googleMap) {
setmap(googleMap);
}
public void setmap(final GoogleMap map) {
googlemap = map;
if (googlemap != null) {
if (sharedPreferences.getBoolean(CustomAlarm.satellite, false)) {
googlemap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else {
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
if(getlocation() == null){
Toast.makeText(getContext(),"You don't live on this earth or Location null",Toast.LENGTH_LONG).show();
}
评论前请注意这一点。
1- 我正在调试我的手机 phone samsung j1 api 19 级。
2- 已启用位置和 gps。
3- 尝试在 googlemaps 中定位我的位置。
4- 还启用了 Google 位置记录。
但是没有任何帮助。
曾经遇到过同样的问题。 我通过请求位置更新解决了这个问题。
Google recommendations about it
一段时间后,位置被捕获。
您正在请求您在这里面的位置
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { }
这意味着如果未授予权限。
行 loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
在授予 IS 权限(可能是)时永远不会执行,因此将保持与您之前分配的值相同 Location loc = null;
所以 null.
您应该将 if 语句更改为
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
这意味着如果位置权限 IS 授予然后给我位置。