1. 权限问题
网页要使用定位,那一定需要相关的定位权限啦:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
目前 app 基本都在 Android6.0 以上了,注意动态申请。
2. WebSettings开启支持
经测试,这两个是必须开启的:
webSettings.setJavaScriptEnabled(true);
webSettings.setGeoLoactionEnabled(true); //这个其实默认为true
许多文章中还增加了以下的配置,我在没有添加的情况下尝试了一下,是可以正常获取位置信息的,应该不是必须。当然,以防万一或者有别的需求要用到,可以加上。
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
String dir = getApplicationContext().getFilesDir().getPath();
settings.setGeolocationDatabasePath(dir);
3. 关于setGeolocationDatabasePath
调用的时候会发现被标注了@Deprecated
被废弃了,我们去源码看一下:
/**
* Sets the path where the Geolocation databases should be saved. In order
* for Geolocation permissions and cached positions to be persisted, this
* method must be called with a path to which the application can write.
*
* @param databasePath a path to the directory where databases should be
* saved.
* @deprecated Geolocation database are managed by the implementation and calling this method
* will have no effect.
*/
@Deprecated
public abstract void setGeolocationDatabasePath(String databasePath);
翻译翻译?
先看看这个方法是干什么的:
设置地理位置数据库的存储路径。为保证定位权限和位置缓存被保存,调用此方法时必须传入应用可写入的路径。
就是用来设置定位权限和位置缓存保存在哪里的,我们再看一下 deprecated 的说明:
@deprecated 定位数据库由实现来管理,调用此方法将没有效果。
也就是说在高版本的系统中,这玩意是由 WebSettings 的实现来管理了,咱们就不用操心了,至于这个方法,调不调用都无所谓,调用了也不会有效果。
那么在什么版本开始我们不用去管它了呢?我们再去谷歌开发者文档看一下:
API 24 也就是 Andorid7.0 开始,此方法废弃了。
那么关于这个方法,我们总结下:
- 如果你的 app 最低版本在 API 24 以上,不需要调用这个方法
- 如果需要支持 7.0 以下的系统,请设置此方法
4. 设置onGeolocationPermissionsShowPrompt回调
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
很简单,调用下invoke方法。我们再看下谷歌的文档:
需要注意的是这一段:
Note that for applications targeting Android N and later SDKs (API level > Build.VERSION_CODES.M) this method is only called for requests originating from secure origins such as https. On non-secure origins geolocation requests are automatically denied.
大致意思就是 Android7.0 开始,只有来自安全的链接如 https 才会回调此方法,不安全的来源系统将自动拒绝定位请求。
考虑到目前都是 Android10 的时代了,所以需要使用 web 定位,请使用 https 连接。
5. 补充
在上面的回调方法中,我们简单的调用了 callback.invoke(origin, true, false);
实际开发中,我们应该考虑权限申请以及定位服务未开启的情况,以提供良好的用户体验。
大致流程是:请求权限 → 判断定位服务是否开启 → (未开启)跳转定位服务设置 → invoke方法
这里提供判断定位服务是否开启和跳转定位服务设置的代码:
/**
* 定位服务是否开启
*/
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
String locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
/**
* 跳转定位服务设置页面
* 用startActivityForResult方式,在onActivityResult中判断是否开启,然后做对应操作
*/
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_CODE);