Posted at

AndroidのWebViewでgeoloactionを使用する

More than 5 years have passed since last update.


位置情報取得用のパーミッションを追加

 <!-- 位置情報取得 -->

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


WebViewClient,WebChromeClientの設定

import android.webkit.GeolocationPermissions.Callback;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setConsentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrLoading(WebView view, String url){
return false;
}
});
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback){
callback.invoke(origin, true, false);
}
});
WebViewSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setGeolocationEnabled(true);

webView.loadUrl("http://~~~");
}