0

navigator.geolocation.getCurrentPosition Cordova API method works with the Monaca Debugger/ App Emulator installed on my iPhone, but freezes in the built iOS version on the same phone.

I added the following to the config.xml but it didn't fix the problem.

<feature name="Geolocation">
    <param name="ios-package" value="CDVLocation" />
</feature> 
4

2 回答 2

0

我只是试图重现你的问题。不幸的是,您发布的信息较少。所以我建立了一个新的cordova项目:

  1. cordova create geolocationTest com.example.com geolocationTest
  2. cd geolocationTest
  3. cordova platform add ios
  4. cordova plugin add cordova-plugin-geolocation
  5. cordova build

然后我进入那个文件夹并编辑了 index.html。我添加了这段代码:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    console.log("navigator.geolocation works well");
}

之后,我添加了 Geolocation-Plugin 文档中的示例:

// onSuccess Callback
// This method accepts a Position object, which contains the
// current GPS coordinates
//
var onSuccess = function(position) {
    alert('Latitude: '          + position.coords.latitude          + '\n' +
          'Longitude: '         + position.coords.longitude         + '\n' +
          'Altitude: '          + position.coords.altitude          + '\n' +
          'Accuracy: '          + position.coords.accuracy          + '\n' +
          'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
          'Heading: '           + position.coords.heading           + '\n' +
          'Speed: '             + position.coords.speed             + '\n' +
          'Timestamp: '         + position.timestamp                + '\n');
};

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

navigator.geolocation.getCurrentPosition(onSuccess, onError);

这一切对我来说都很好。所以问题必须在你的代码中的任何地方。您应该提供更多信息。

于 2015-04-23T06:52:36.780 回答
0

我想我找到了问题所在。iOS8 在应用 plist 中需要以下内容:

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>

并且 locationManager.requestAlwaysAuthorization 或 locationManager.requestWhenInUseAuthorization 需要在 navigator.geolocation.getCurrentPosition 被调用之前被调用。下面解释了 iOS8 的变化:http: //nevan.net/2014/09/core-location-manager-changes-in-ios-8/

我现在遇到的当前问题是在 Monaca 环境中成功调用 locationManager.requestAlwaysAuthorization 或 locationManager.requestWhenInUseAuthorization。

于 2015-04-24T02:10:37.853 回答