Android 安卓谷歌地图的位置可用
我有一个Android 安卓谷歌地图的位置可用,android,google-maps,Android,Google Maps,我有一个GoogleMap的实例,它运行得很好。如何确定它是否可以检索当前位置? 我有一个方法可以识别位置是否可用,但即使我将手机设置为飞行模式,它也会返回true,但我的位置不会更新。有没有办法检测谷歌地图是否可以访问我的位置,以及应该采取什么行动来引导用户启用网络 以下是我访问网络的代码: public static boolean isLocationEnabled(Context context) { LocationManager lm = null; boolean
GoogleMap
的实例,它运行得很好。如何确定它是否可以检索当前位置?
我有一个方法可以识别位置是否可用,但即使我将手机设置为飞行模式,它也会返回true,但我的位置不会更新。有没有办法检测谷歌地图是否可以访问我的位置,以及应该采取什么行动来引导用户启用网络
以下是我访问网络的代码:
public static boolean isLocationEnabled(Context context)
{
LocationManager lm = null;
boolean gps_enabled = false;
boolean network_enabled = false;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (lm != null)
{ gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
if(!gpsEnabled && !networkEnabled){
DevLog.e("NETWORK NOT AVAILABLE");
return false;
}
else
{
DevLog.e("NETWORK IS AVAILABLE", gpsEnabled, networkEnabled);
return true;
}
}
Google Maps API具有位置功能,您可以使用该功能进行操作,例如:
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
if(mMap != null){
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
}
};
然后设置映射的侦听器:
mMap.setOnMyLocationChangeListener(myLocationChangeListener);
当地图第一次找到位置时,将调用此函数
根本不需要LocationService或LocationManager。您需要了解的是,GPS不同于网络,因此即使您将手机置于飞行模式,GPS也会工作并为您提供位置信息 尝试按如下方式更改代码
if (lm != null){
gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
if(gpsEnabled && isNetworkAvailable()){
DevLog.e("NETWORK IS AVAILABLE", gpsEnabled, networkEnabled);
return true;
} else if(!isNetworkAvailable()){
DevLog.e("NETWORK NOT AVAILABLE");
return false;
} else if(!gpsEnabled ){
DevLog.e("GPS NOT AVAILABLE");
return false;
} else{
DevLog.e("GPS & NETWORK ARE NOT AVAILABLE");
return false;
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
这对你有用
通过将手机切换到飞行模式进行检查 请注意,OnMyLocationChangeListener接口已被弃用。因此,它不应该再被使用了。我很惊讶它是去润滑的。我应该用什么来代替它?而且它也不能帮助我理解是否可以检索当前位置。请在设备上禁用GPS并处于活动飞行模式的情况下尝试。DevLog.e(“网络可用”,gpsEnabled,networkEnabled);这句话说网络也可用,但它不应该可用。日志说,即使我将手机置于飞行模式,它们都可用,关闭所有可能的网络不要使用LocationManager获取网络状态。这仅适用于GPS(位置)。使用连接管理器。检查我编辑的答案。