地理定位无法在Android模拟器

时间:2017-05-20 12:13:39

标签: android android-studio geolocation

使用Android Studio的Android模拟器时,Geolocation不起作用。我尝试了在这里发布的所有解决方案,但似乎没有任何效果。代码很好,因为它在我的实际设备上运行完美。这是我正在使用的HTML页面:

 <!DOCTYPE html>
<html>
<head>
    <script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true });
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    var latlon = position.coords.latitude + "," + position.coords.longitude;
    document.getElementById("locationHolder").innerHTML = latlon;
}

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            console.log("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            console.log("Location information is unavailable");
            break;
        case error.TIMEOUT:
           console.log("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
            console.log("An unknown error occurred.");
            break;
    }
}
</script>
</head>

<body>

<p id="demo">Click the button to get your position.</p>

<div id="locationHolder">No location</div>
<button onclick="getLocation()">Get position</button>

</body>
</html>

这是webview的代码:

public void openWebView(){
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        WebSettings webSettings = myWebView.getSettings();
        CookieManager.getInstance().setAcceptCookie(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true); 
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setGeolocationEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        myWebView.setWebChromeClient(new WebChromeClient() {
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }
        });
        myWebView.loadUrl("file:///android_asset/www/test.html");

        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.toString());
                return true;
            }


        });



    }

这是日志的输出:

05-20 08:09:23.442 28860-29006/com.app E/SQLiteLog: (14) cannot open file at line 30184 of [00bb9c9ce4]
05-20 08:09:23.442 28860-28881/com.app E/SQLiteLog: (14) cannot open file at line 30184 of [00bb9c9ce4]
05-20 08:09:23.442 28860-28881/com.app E/SQLiteLog: (14) os_unix.c:30184: (2) open(/GeolocationPermissions.db) - 
05-20 08:09:23.442 28860-29006/com.app E/SQLiteLog: (14) os_unix.c:30184: (2) open(/CachedGeoposition.db) - 
05-20 08:09:23.452 28860-28860/com.app I/Web Console: Location information is unavailable at file:///android_asset/www/test.html:26

Nexus 4 API 18会出现此问题,但我也尝试过其他模拟器。我为Google API选择了这些模拟器。

0 个答案:

没有答案