【问题标题】:Convert Latitude and Longitude Values (Decimals) to Double将纬度和经度值(小数)转换为双精度
【发布时间】:2017-10-01 22:49:00
【问题描述】:

我目前正在使用这个插件 https://github.com/sebastianbaar/cordova-plugin-nativegeocoder 在科尔多瓦/离子应用程序和 iOS 上,我收到一条错误消息,指出我需要提供双倍坐标。

文档清楚地说明了这一点,但是因为我使用https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/ 来获取 ios 上的纬度和经度,这些以小数形式返回并且很长,并且不确定如何在 javascript 中执行此操作。我一直在这里搜索,即:https://stackoverflow.com/search?q=double+latitude+and+longitude,但这些答案都不是很清楚,也不是在 javascript / angularjs 中。这是我如何获取坐标的代码。

$cordovaGeolocation.getCurrentPosition().then(function (position) {
    var lat  = position.coords.latitude
    var long = position.coords.longitude

谢谢

【问题讨论】:

  • latlong 字符串吗?也许您只需要 +position.coords.latitude 将值强制为 Number。 Javascript 没有浮点数、整数等类型,它只有Number

标签: javascript angularjs ionic-framework double cordova-plugins


【解决方案1】:
 $cordovaGeolocation.getCurrentPosition().then(function (position) { 
   var lat  = position.coords.latitude
   var long = position.coords.longitude
 });

在上面的代码中,它可以在字符串格式中得到 lat long,因此它会给你错误

以下代码修复它

 $cordovaGeolocation.getCurrentPosition().then(function (position) { 
   var lat  = position.coords.latitude
   var long = position.coords.longitude
   var latDouble  = parseFloat(lat);
   var longDouble = parseFloat(long);
 });

如何检查类型是否正确?

在 console.log() 中打印;

如果是字符串,则打印为“21.4564879”

如果为双精度则直接显示 21.4564879

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多