【问题标题】:google maps api - error -Assertion failed: InvalidValueError: in property latLng: not a LatLng or LatLngLiteral: in property lat: not a numbergoogle maps api-错误-断言失败:InvalidValueError:在属性latLng中:不是LatLng或LatLngLiteral:在属性lat:不是数字
【发布时间】:2016-01-03 13:31:46
【问题描述】:

我正在尝试从给定 lat、lng 中获取 formatted_address,但我遇到错误“断言失败:InvalidValueError:在属性 latLng:不是 LatLng 或 LatLngLiteral:在属性 lat:不是数字” 我正在使用 GMap.js 下面是我的代码: 首先我得到用户的当前位置

 GMaps.geolocate({
            success: function (position) {
getSetFormattedAddressLatLng( {
                    H: position.coords.latitude,
                    L: position.coords.longitude
                });
            },
            error: function (error) {
                notificationService.error('Geolocation failed: ' + error.message);
            },
            not_supported: function () {
                alert("Your browser does not support geolocation");
            },
            always: function () {
                //alert("Done!");
            }
        });

调用getSetFormattedAddressLatLng()函数获取用户当前位置成功后,函数如下所示

function getSetFormattedAddressLatLng(latLng) {
      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({
                    latLng: latLng
                }, function (responses) {
                    if (responses && responses.length > 0) {
                        //set the formatted_address on originOrDestination               

                    } else {
                        debugger;
                        notificationService.error('Cannot determine address at this location.');
                    }
                });
}

但是当我调用 geocoder.geocoder() 时,我收到错误消息“断言失败:InvalidValueError:在属性 latLng:不是 LatLng 或 LatLngLiteral:在属性 lat:不是数字”。 我做错了什么?

问候, 赤胆

【问题讨论】:

标签: google-maps google-maps-api-3


【解决方案1】:

感谢邓肯的提示,解决了。 通过如下创建 latLng 对象然后将其传递给 geocoder.geocode() 来解决

var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

【讨论】:

    【解决方案2】:

    您将这样的结构传递给geocoder.geocode 构造函数:

    {
       H: position.coords.latitude,
       L: position.coords.longitude
    }
    

    这是无效的。尝试这样做(我假设 position 已经是一个 LatLng 对象):

    getSetFormattedAddressLatLng(position);
    

    或者可能

    getSetFormattedAddressLatLng(position.coords);
    

    在最坏的情况下,您可以创建一个新的 LatLng 对象:

    GMaps.geolocate({
            success: function (position) {
                getSetFormattedAddressLatLng(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
            },
    

    【讨论】:

    • 不,仍然是同样的错误,“位置”我意识到是地理定位的构造函数。我意识到 latLng 应该是一个构造函数,但我不知道哪个函数,你能帮我吗?
    猜你喜欢
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多