簡體   English   中英

計算距離時 JavaScript 未捕獲類型錯誤

[英]JavaScript Uncaught Type Error while computing distance

我只是想計算兩點之間的距離,但它給了我 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 是從導航器對象中獲取的用戶位置。 問題是當我刪除計算距離時一切正常。

google.maps.geometry.spherical.computeDistancBetween方法不適用於LatLngLiteral對象(至少目前),您必須傳入google.maps.LatLng對象。

來自(最新)文檔

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

返回值:數字

返回兩個 LatLng 之間的距離(以米為單位)。 您可以選擇指定自定義半徑。 半徑默認為地球的半徑。

你有一個語法錯誤。 有一個帶逗號的最后一個 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],

            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM