【发布时间】:2013-10-25 17:18:47
【问题描述】:
我正在尝试构建一个 android 应用程序,我相处得很好,但我在通过 webview 查看的网页调用 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='@987654322@'+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();
}
}
}
【问题讨论】:
-
你能显示你调用变量的代码吗?
-
你指的是哪一个? php部分?我不认为它是必要的,因为它根本不会到达那一点......它不会重新加载页面
标签: javascript android webview geolocation