【发布时间】:2015-05-31 20:57:22
【问题描述】:
我正在研究cordova app,我必须在其上locate the user 纬度和经度。
使用地理定位插件,它可以在 android 设备上正常工作,但它会显示一条警报,要求iOS 中的用户许可。当我使用模拟器时,我收到以下警告消息:
Users/user/Library/Developer/CoreSimulator/Devices/783A2EFD-2976-448C-8E4E-841C985D337D/data/Containers/Bundle/Application/EFC846BB-4BA3-465C-BD44-575582E649FC/app_name.app/www/index.html would like to use your current location.
我看到过这样的话题谈论这个问题:this 和 this但是提供的解决方案都不适合我。
这是科尔多瓦示例页面:
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>
有什么方法可以更改警报的文本或禁用此警报?
-编辑---
我找到了问题的根源。我删除了地理定位插件并添加了几次,因为当我添加插件时,我没有像其他插件一样找到名称为 geolocation plugin 的文件夹。即使cordova_plugin.js 文件也不包含任何有关地理定位插件的数据。现在我再次安装了插件,它可以工作了。
【问题讨论】:
-
不要在 deviceready 被触发后立即调用 getCurrentPosition。您是否尝试过 setTimeout 在延迟后调用 getCurrentPosition?
-
是的,即使使用 setTimeout(function(){ navigator.geolocation.getCurrentPosition(onSuccess, onError); }, 60000);丑陋的 HTML 消息正在显示。
-
@Dev DOS 如何删除此错误
-
@nivritgupa 我不记得究竟是怎么回事,但我记得这是一个与地理定位插件有关的问题,所以:删除/添加地理定位插件:
cordova plugin rm org.apache.cordova.geolocation cordova plugin add org.apache.cordova.geolocation将 NSLocationWhenInUseUsageDescription 添加到 .plist。打开 /platforms/ios/{project}/{project}-Info.plist 并添加以下内容:<key>NSLocationWhenInUseUsageDescription</key> <string>[App Name] would like to access your location when running and displayed.</string>
标签: javascript ios iphone cordova-4