所以我们制作了一个使用地理数据的html5网络应用程序。
当我在本地chrome浏览器上打开它时,它会显示所有位置和我的当前位置。
如果我打开它的应用程序,它显示所有的位置,但不是我的。。。我怎样才能解决这个问题?
代码如下:
public class MainActivity extends Activity {
/**
* WebViewClient subclass loads all hyperlinks in the existing WebView
*/
public class GeoWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// When user clicks a hyperlink, load in the existing WebView
if(url.startsWith("geo:"))
{
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
return true;
}
view.loadUrl(url);
return true;
}
}
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, false, false);
}
}
WebView mWebView;
/**Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
// Brower niceties -- pinch / zoom, follow links in place
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new GeoWebViewClient());
// Below required for geolocation
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.setWebChromeClient(new GeoWebChromeClient());
mWebView.loadUrl("http://insert-website-here.de");
}
@Override
public void onBackPressed() {
// Pop the browser back stack or exit the activity
if (mWebView.canGoBack()) {
mWebView.goBack();
}
else {
super.onBackPressed();
}
}
}
我想这会解决的:
public class GeoWebChromeClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
// Always grant permission since the app itself requires location
// permission and the user has therefore already granted it
callback.invoke(origin, false, false);
}
}
但看起来不像…有什么想法吗?
以下是我的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1条答案
按热度按时间1u4esq0p1#
必须在webview中启用javascript,使用
WebSettings.setJavaScriptEnabled(true);
这个WebView
必须使用自定义WebChromeClient
哪个实现了WebChromeClient.onGeolocationPermissionsShowPrompt().
此方法由WebView
获取向javascript公开用户位置的权限(在浏览器的情况下,我们向用户显示一个提示。)默认实现不做任何操作,因此永远不会获得权限,也不会将位置传递给javascript。一个总是授予权限的简单实现是。。。数据库的位置是使用
WebSettings.setGeolocationDatabasePath(...)
```webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );