gpt4 book ai didi

javascript - Google Maps API 方向服务未显示超过 10 个标记

转载 作者:行者123 更新时间:2023-11-28 04:25:59 25 4
gpt4 key购买 nike

我正在尝试将 map 上的不同标记链接在一起。目前,这些标记已被硬编码,但平板电脑将及时发送其 GPS 位置,并将其输入网站。无论出于何种原因,如果我输入超过 10 个标记,折线就不再显示。有人可以帮我弄清楚为什么会发生这种情况吗?谢谢。

<html>
<head>
</head>

<body>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
var map;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var locations = [
['16:58', -20.1672799707871, 57.5069032768888],
['17:00', -20.170670813731, 57.5032840805832],
['17:01', -20.1712602810727, 57.502503950733],
['17:04', -20.166955925865, 57.4892648490453],
['17:07', -20.1761991610139, 57.4822988090064],
['17:10', -20.1961753165077, 57.4824656045157],
['17:13', -20.220309405427, 57.487469732469],
['17:16', -20.2342861943874, 57.4996038055266],
['17:19', -20.2419951166671, 57.4924547310887],
['17:22', -20.2418197976697, 57.4838445649936],
['17:25', -20.2417927882899, 57.4821667460937],
['17:28', -20.244868944177, 57.4761929726994],
['17:31', -20.2494147017248, 57.4751336596163],
['17:34', -20.2529453707358, 57.4698643969492],
['17:37', -20.2515653985995, 57.4694827601669]
];

function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();


var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(-20.23, 57.49),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
directionsDisplay.setMap(map);
var infowindow = new google.maps.InfoWindow();

var marker, i;
var request = {
travelMode: google.maps.TravelMode.DRIVING
};
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));

if (i == 0) request.origin = marker.getPosition();
else if (i == locations.length - 1) request.destination = marker.getPosition();
else {
if (!request.waypoints) request.waypoints = [];
request.waypoints.push({
location: marker.getPosition(),
stopover: true
});
}

}
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
</script>
<div id="map" style="width: 100%; height: 100%">
</div>
</body>
</html>

最佳答案

您绘制的折线宽度不能超过 8 点加上起点和终点:

Google Maps Directions :

MAX_WAYPOINTS_EXCEEDED indicates that too many waypoints were provided in the request The maximum allowed waypoints is 8, plus the origin, and destination. ( Google Maps API for Work customers may contain requests with up to 23 waypoints.)

这是因为免费 API 仅限于:

Users of the free API get Up to 8 waypoints (plus origin and destination) allowed in each request and a total of 2,500 directions requests per 24 hour period.

尝试将折线拆分为多个请求

关于javascript - Google Maps API 方向服务未显示超过 10 个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056570/

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