我有一个使用webview的应用程序,并且webview内有一个地图,我可以使用以下代码来让用户自动找到用户的位置:
清单文件权限:
<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" />
WebView活动:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_layout); WebViewSettings(); LoadWebPage(urlString); }
WebView设置:
@SuppressLint("SetJavaScriptEnabled") public void WebViewSettings(){ webView = (WebView) findViewById(R.id.webview); webView.canGoBack(); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setSupportZoom(true); webView.getSettings().setLoadWithOverviewMode(true); webView.setWebViewClient(new WebViewClientSetup()); webView.setWebChromeClient(new GeoWebChromeClient()); webView.getSettings().setGeolocationEnabled(true); myTimer = new Timer(); //Start this timer when you create you task myTimer.schedule(new loaderTask(), 20000); }
WebViewClient:
public class WebViewClientSetup extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } @Override public void onLoadResource(WebView view, String url) { // Check to see if there is a progress dialog if (progressDialog == null) { progressDialog = new ProgressDialog(context); progressDialog.setTitle("Loading..."); progressDialog.setMessage("Please wait."); progressDialog.setCancelable(true); progressDialog.setIndeterminate(true); progressDialog.show(); } } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); } @Override public void onPageFinished(WebView view, String url) { isPageLoadedComplete = true; View mapHeight = (View) findViewById(R.id.webview); int height = mapHeight.getHeight(); Log.d("map height", "map height" + height); // Page is done loading; // hide the progress dialog and show the webview if (progressDialog.isShowing()) { progressDialog.dismiss(); } } @Override public void onReceivedError(WebView view, int errorCod, String description, String failingUrl) { AlertDialog(); } }
WebChromeClient:
public class GeoWebChromeClient extends WebChromeClient { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } }
因此,这是可行的,但是它会自动获取用户的位置,而不会提示用户该应用程序是否可以使用位置服务。
有没有办法将此功能添加到应用程序?谢谢
那是因为您需要创建提示。onGeolocationPermissionsShowPrompt简单地调用该函数是为了让您知道网页需要该位置。
onGeolocationPermissionsShowPrompt
callback.invoke(origin, true, false); 是告诉WebView可以使用该位置的响应。
callback.invoke(origin, true, false);
最后一个参数是是否要记住此设置。参见:http : //developer.android.com/reference/android/webkit/GeolocationPermissions.Callback.html
例:
AlertDialog.Builder adb = new AlertDialog.Builder(context); ..... //set up title, message, etc adb.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { callback.invoke(origin, false, false); } }); adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { callback.invoke(origin, true, false); } }); adb.show();