gpt4 book ai didi

javascript - 谷歌地图javascript绘制折线

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

我正在尝试通过 Google map 绘制折线。我已经通过捕捉到道路功能获得了一条路径,但我将其作为对象获取,但我需要一个数组作为路径。知道如何将路径对象转换为数组吗?这是我到目前为止得到的代码:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body { height: 100%; margin: 0; padding: 0; }
#map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
var map;

function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 65, lng: -20},
zoom: 8
});
path = (64.06507, -21.57787),(64.06324000000001, -21.567200000000003),(64.06213000000001, -21.560760000000002),(64.06129, -21.555650000000004),(64.06070000000001, -21.55158);
var geralinu = new google.maps.Polyline({
path: path,
strokeColor: 'Red',
strokeOpacity: 1.0,
strokeWeight: 2
});
//geralinu.setPath(lina);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyClRZNMxcuO2BSi3nynNQu7e7uFyzylWZ4&callback=initMap">
</script>
</body>
</html>

我收到的错误是 Uncaught TypeError: Object.entries 不是函数

最佳答案

这不是有效的 JavaScript:

path = (64.06507, -21.57787),(64.06324000000001, -21.567200000000003),(64.06213000000001, -21.560760000000002),(64.06129, -21.555650000000004),(64.06070000000001, -21.55158);

我建议制作一个 google.maps.LatLngLiteral 数组对象(我删除了末尾的一些额外的零):

var path = [{lat: 64.06507,lng: -21.57787}, {lat: 64.06324,lng: -21.5672}, {lat: 64.06213,lng: -21.56076}, {  lat: 64.06129,lng: -21.55565}, {lat: 64.0607,lng: -21.55158}];

proof of concept fiddle

代码片段:

var map;

function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 65,
lng: -20
},
zoom: 8
});
var path = [{lat: 64.06507,lng: -21.57787},
{lat: 64.06324,lng: -21.5672},
{lat: 64.06213,lng: -21.56076},
{lat: 64.06129,lng: -21.55565},
{lat: 64.0607,lng: -21.55158}];

var geralinu = new google.maps.Polyline({
path: path,
strokeColor: 'Red',
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
});
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < geralinu.getPath().getLength(); i++) {
bounds.extend(geralinu.getPath().getAt(i));
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

关于javascript - 谷歌地图javascript绘制折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35213912/

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