地理定位在 Cordova Android 模拟器中不起作用

Geolocation not working in Cordova Android emulator

我正在使用 visual studio Cordova 工具创建应用程序。我正在使用 HTML5 的地理定位来获取用户位置。

当我在 ripple naxus-galaxy 中执行它时,它工作正常,但是当我在 android 模拟器中 运行 它时,它根本不工作。它在顶部栏上显示了 GPS 系统。但在那之后什么也没有发生。我还添加了 geoLocation 插件。

我的代码是,

    // For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=397704
// To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints, 
// and then run "window.location.reload()" in the JavaScript Console.
(function () {
    "use strict";

    document.addEventListener('deviceready', onDeviceReady.bind(this), false);

    function onDeviceReady() {
        // Handle the Cordova pause and resume events
        document.addEventListener('pause', onPause.bind(this), false);
        document.addEventListener('resume', onResume.bind(this), false);

        // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.

        var geo_options = {
            enableHighAccuracy: true,
            maximumAge: 50000,
            timeout: 30000
        };

        var mapWatchId = navigator.geolocation.watchPosition(onSuccess, onError, geo_options);
        alert("Watch" + mapWatchId);
    };


    function onSuccess(position) {
        alert("onSuccess = " + JSON.stringify(position));
    };

    function onError(error) {
        alert("Error");
        switch (error.code) {
            case error.PERMISSION_DENIED:
                alert("Please share your location with us to move ahead.");
                break;
            case error.POSITION_UNAVAILABLE:
                alert("Location information is not available, Check your internet connection.");
                break;
            case error.TIMEOUT:
                alert("The request to get user location timed out.");
                break;
            case error.UNKNOWN_ERROR:
                alert("We are not able to fetch your location details.");
                break;
        }
    };
    function onPause() {
        // TODO: This application has been suspended. Save application state here.
    };

    function onResume() {
        // TODO: This application has been reactivated. Restore application state here.
    };
})();

我的配置文件是,

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="io.cordova.Geo" version="1.0.0.0" xmlns="http://www.w3.org/ns/widgets">
  <name>Geo</name>
  <description>A blank project that uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
  <author href="http://cordova.io" email="dev@cordova.apache.org">Apache Cordova Team </author>
  <content src="index.html" />
  <access origin="*" />
  <preference name="SplashScreen" value="screen" />
  <preference name="windows-target-version" value="8.0" />
  <preference name="windows-phone-target-version" value="8.1" />
  <vs:plugin name="org.apache.cordova.geolocation" version="0.3.10" />
  <vs:platformSpecificValues />
</widget>

甚至,它根本没有调用 gMap() 函数,因为 alert 本身不会来。

请检查上面的代码并告诉我必要的更改。

谢谢。

您无法在 android 模拟器中测试地理定位模式,推送通知也是如此。您需要在真实的移动设备中进行测试。