【问题标题】:JavaScript Uncaught Type Error while computing distance计算距离时 JavaScript 未捕获类型错误
【发布时间】:2016-10-08 02:12:14
【问题描述】:

我只是想计算两点之间的距离,但它给了我 Uncaught TypeError: a.lat is not a function。

 function MapLocations() {
        
            var i = 0;
            var infoWindow = new google.maps.InfoWindow();
            	var myLatLng = {lat: 31.553761202565646, lng: 74.26506623625755}
                var m = new google.maps.Marker({
                    map: map,
                    clickable: true,
                    animation: google.maps.Animation.DROP,
                    title: 'My Home',
                    position: myLatLng,
                   // html: h[i],
                    
                });

                var circle = new google.maps.Circle({
                    map: map,
                    radius: 18.288,    // 10 miles in metres
                    fillColor: '#50D050'
                });

                circle.bindTo('center', m, 'position');
                
           		var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(myLatLng, pos).toFixed(2);
           		//console.log(distanceInMetres);
                //google.maps.event.addListener(m, 'click', function () {
                //    infoWindow.setContent('Hello');
                //    infoWindow.open(map, this);
                //});
                //google.maps.event.addListener(dragable_marker, 'dragend', function (e) {
                //    alert(circle.getBounds().contains(dragable_marker.getPosition()));
                //});

               //alert(distanceInMetres);
                i++;

            
        }

pos 是从导航器对象中获取的用户位置。问题是,当我删除 computedistance 时,一切正常。

【问题讨论】:

标签: javascript google-maps


【解决方案1】:

google.maps.geometry.spherical.computeDistancBetween 方法不适用于LatLngLiteral 对象(至少目前如此),您必须传入google.maps.LatLng 对象。

来自the (latest) documentation

computeDistanceBetween(从:LatLng,到:LatLng,radius?:number)

返回值:数字

返回两个 LatLng 之间的距离(以米为单位)。您可以选择指定自定义半径。半径默认为地球的半径。

【讨论】:

    【解决方案2】:

    你有一个 sintax 错误。有一个带逗号的最后一个 javascript 对象属性

               var m = new google.maps.Marker({
                    map: map,
                    clickable: true,
                    animation: google.maps.Animation.DROP,
                    title: 'My Home',
                    position: myLatLng,
                                     ??? this is wrong
                   // html: h[i],
    
                });
    

    你应该在没有,的情况下使用它

               var m = new google.maps.Marker({
                    map: map,
                    clickable: true,
                    animation: google.maps.Animation.DROP,
                    title: 'My Home',
                    position: myLatLng
    
                   // html: h[i],
    
                });
    

    【讨论】:

    • 正如我所说的,问题在于 computeDistanceBetween 函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2019-02-20
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    相关资源
    最近更新 更多