Androidアプリを構築しようとしていますが、うまくやっていますが、Webビューで表示しているWebページからjavascript関数を呼び出すのに問題があります.
このチュートリアルを使用して、webview で地理位置情報を取得しました: http://turbomanage.wordpress.com/2012/04/23/how-to-enable-geolocation-in-a-webview-android/
今、私は自分のページ内でこの関数を呼び出したい:
var t=setTimeout("navigator.geolocation.getCurrentPosition(foundLocation);",15000);
function foundLocation(position)
{
var lat = position.coords.latitude;
var long = position.coords.longitude;
window.location.href='http://rittenservice.nl/rittensysteem2.php?lat='+lat+'&long='+long+'';
}
この関数が呼び出されると、URL を使用してページをリロードする必要があるため、PHP で場所を保存できます
なぜそれが機能しないのか、誰にも分かりますか?
ウェブビュー コード:
public class GeoWebViewActivity extends Activity {
public class GeoWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("androidurl://")) {
url = url.replaceAll("androidurl://", "http://");
}
// When user clicks a hyperlink, load in the existing WebView
view.loadUrl(url);
return true;
}
}
/**
* WebChromeClient subclass handles UI-related calls
* Note: think chrome as in decoration, not the Chrome browser
*/
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, true, false);
}
}
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_geo_web_view);
mWebView = (WebView) findViewById(R.id.webView1);
// 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());
// Load google.com
mWebView.loadUrl("http://www.rittenservice.nl/keypad.php");
}
@Override
public void onBackPressed() {
// Pop the browser back stack or exit the activity
if (mWebView.canGoBack()) {
mWebView.goBack();
}
else {
super.onBackPressed();
}
}
}