Cordova PhoneGap地理定位对象没有';你不喜欢四舍五入吗?

Cordova PhoneGap地理定位对象没有';你不喜欢四舍五入吗?,cordova,geolocation,rounding,tofixed,Cordova,Geolocation,Rounding,Tofixed,我创建了一个演示PhoneGap应用程序,它检查加速度、指南针和地理位置,并将这些信息呈现给用户。应用程序是使用PhoneGap构建编译的 我使用simpletoFixed(n)和一些字符串对值进行四舍五入,并在值后添加一些单位。这在加速度和指南针的情况下非常有效。出于我想象之外的某些原因,在地理定位的情况下(至少在Galaxy Nexus/Android 4.3上)这种方法会失败 我使用了这样的功能: function onGeolocationSuccess(position) {

我创建了一个演示PhoneGap应用程序,它检查加速度、指南针和地理位置,并将这些信息呈现给用户。应用程序是使用PhoneGap构建编译的

我使用simple
toFixed(n)
和一些字符串对值进行四舍五入,并在值后添加一些单位。这在加速度和指南针的情况下非常有效。出于我想象之外的某些原因,在地理定位的情况下(至少在Galaxy Nexus/Android 4.3上)这种方法会失败

我使用了这样的功能:

function onGeolocationSuccess(position)
{
    console.log(position);

    var coords = position.coords;

    coords.latitude = coords.latitude.toFixed(6);
    coords.longitude = coords.longitude.toFixed(6);
    coords.altitude = coords.altitude.toFixed(2) + ' m';
    coords.accuracy = coords.accuracy.toFixed(6) + ' m';
    coords.altitudeAccuracy = coords.altitudeAccuracy.toFixed(2) + ' m';
    coords.heading = coords.heading.toFixed(2) + '\u00b0';
    coords.speed = coords.speed.toFixed(2) + ' m/s';

    var geoText = 'Longitude is ' + coords.longitude + '.';

    geoText = geoText + 'Latitude is ' + coords.latitude + '.';
    geoText = geoText + 'Accuracy is ' + coords.accuracy + '.';
    geoText = geoText + 'Altitude is ' + coords.altitude + '.';
    geoText = geoText + 'Altitude Accuracy is ' + coords.altitudeAccuracy + '.';
    geoText = geoText + 'Heading is ' + coords.heading + '.';
    geoText = geoText + 'Speed is ' + coords.speed + '.';

    $('#positionText').html(geoText);
}
它作为PhoneGap的
navigator.geolocation.watchPosition的
success
回调传递

函数启动,整个位置对象被写入控制台,仅此而已。如果
var coords=position.coords之间的行
var geoText='经度为'+coords.Longitude+'未注释,函数执行在中间某处终止,行
$('#positionText').html(geoText)未被执行,并且
positionText
div未正确更新

如果我把这七行“舍入”注释掉,一切都很好,div正在更新。在加速度和指南针物体的情况下,同样的圆像一种魅力,这使得这更奇怪


谁能告诉我,我错过了什么?为什么PhoneGap geolocation对象不喜欢舍入,而compass和acceleration对象可以这样做?

您的position对象的一部分返回空值,当toFixed()应用于这些空值时,这会杀死您的代码。下面是我如何让它工作的:

HTML

<div id="positionText"></div>

Javascript

function onGeolocationSuccess(position) {

    var coords = {};

    coords.latitude = position.coords.latitude.toFixed(6);
    coords.longitude = position.coords.longitude.toFixed(6);
    coords.altitude = position.coords.altitude + ' m';
    coords.accuracy = position.coords.accuracy + ' m';
    coords.altitudeAccuracy = position.coords.altitudeAccuracy + ' m';
    coords.heading = position.coords.heading + '\u00b0';
    coords.speed = position.coords.speed + ' m/s';

    var geoText = 'Longitude is ' + coords.longitude + '.<br/>';

    geoText += 'Latitude is ' + coords.latitude + '.<br/>';
    geoText += 'Accuracy is ' + coords.accuracy + '.<br/>';
    geoText += 'Altitude is ' + coords.altitude + '.<br/>';
    geoText += 'Altitude Accuracy is ' + coords.altitudeAccuracy + '.<br/>';
    geoText += 'Heading is ' + coords.heading + '.<br/>';
    geoText += 'Speed is ' + coords.speed + '.<br/>';

    $('#positionText').html(geoText);
}

function onError() {

}

navigator.geolocation.getCurrentPosition(onGeolocationSuccess, onError);
功能定位成功(位置){
var-coords={};
坐标纬度=位置坐标纬度固定(6);
坐标经度=位置坐标经度固定(6);
坐标系高度=位置坐标系高度+m';
坐标精度=位置坐标精度+m;
coords.altitudeAccuracy=位置.coords.altitudeAccuracy+'m';
coords.heading=position.coords.heading+'\u00b0';
坐标速度=位置坐标速度+米/秒;
var geoText='经度为'+coords.Longitude+'。
'; geoText+='纬度为'+coords.Latitude+'。
'; 土工织物+='精度为'+coords.Accurance+'。
'; geoText+=“高度为”+coords.Altitude+”。
; geoText+=“高度精度为”+coords.altitudeAccuracy+”。
; geoText+=“标题为”+coords.Heading+”。
; geoText+=“速度为”+coords.Speed+”。
; $('#positionText').html(geoText); } 函数onError(){ } navigator.geolocation.getCurrentPosition(OnGeolocationsSuccess,onError);
位置对象的任何部分是否返回空值?我将您的代码放在一个JSFIDLE中,对其进行了一些处理,使其工作正常。只有在尝试对空值执行.toFixed()时,它才会中断。PhonegapSeems没有问题,这一点你已经明白了(虽然如此明显!)。请将此评论重新表述为完整的答案,这样我就可以接受它并奖励您的作品,并附上一些代表点。