gpt4 book ai didi

javascript - 单击 map 中的某个点会给出错误的纬度和经度?

转载 作者:行者123 更新时间:2023-12-05 06:32:54 26 4
gpt4 key购买 nike

我在谷歌地图中创建了一条简单的折线。我还向多段线添加了点击事件监听器。

问题是,当我点击这条线时,它给出了加拿大某处的经纬度坐标,即使折线在美国。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>

// This example creates a 2-pixel-wide red polyline showing the path of
// the first trans-Pacific flight between Oakland, CA, and Brisbane,
// Australia which was made by Charles Kingsford Smith.

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 30,
center: {lat: 38.582692796303924, lng: -89.9953046014092},
mapTypeId: 'satellite'
});



var flightPlanCoordinates = [
{lat: 38.582692796303924, lng: -89.9953046014092},
{lat: 38.582663144388235, lng: -89.99447848103262}
];

var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
icons: [{icon: {path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW}, offset: '100%'}],
geodesic: true,
strokeColor: 'red',
strokeOpacity: 1.0,
strokeWeight: 2
});

flightPath.setMap(map);


google.maps.event.addListener(flightPath, 'click', function(event) {

var arr = this.getPath();
//coordinates of start and end path points
console.log(arr.getAt(0).lat() + ', ' + arr.getAt(0).lng());
console.log(arr.getAt(1).lat() + ', ' + arr.getAt(1).lng());

//coordinates of clicked point
console.log(event.latLng.lat() + ", " + event.latLng.lng());

});



flightPath.setMap(map);
}
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=validkey&libraries=geometry&callback=initMap" async defer></script>


</script>
</body>
</html>

请按照以下步骤重现问题。

1) First save the above code in html file and replace validkey to a valid google maps api key. 
2) When the map displays it should already be at maximum zoom level and in satellite mode.
3) Now click F12 to open developer tools console.
4) Click on somewhere between the polyline.
5) It prints three lines.

enter image description here

可以看到最后一行应该是点击点的坐标。它给出的纬度和经度是在加拿大的某个地方。

我很感激任何反馈。谢谢!

最佳答案

我遇到了同样的问题。这是我的工作......

我有一个信息窗口,我希望它显示在我单击多段线的位置。在鸟瞰图中工作正常,但在倾斜(3d) View 中测试时它得到了错误的坐标。我在监听器上使用 event.latLng 进行折线点击。所以我所做的是在折线路径的中间点倾斜时覆盖 latLng。

之前的代码:

google.maps.event.addListener(polyline, 'click', function(event) { 
openInfoWindow(event.latLng, infoWindowPolyline);
});

之后的代码:

google.maps.event.addListener(polyline, 'click', function(event) { 
latLng = ((map.getTilt() == 0) ? event.latLng : getMiddlePoint(polyline.getPath()) );
openInfoWindow(latLng, infoWindowPolyline);
});

function getMiddlePoint(pathArr) {
pathLength = pathArr.length;

if (pathLength <= 2) {
index = 1;
} else {
index = Math.ceil(pathArr.length / 2);
}
myLatlng = { lat: pathArr.getAt(index).lat(), lng: pathArr.getAt(index).lng() };
return myLatlng;
}

关于javascript - 单击 map 中的某个点会给出错误的纬度和经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51036365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com