我在这里进行的是,我正在创建一个以点为中心的 Map (完美地工作),绘制由 journey
变量指定的一堆点(值由django模板替换),然后尝试绘制多边形在这些点之间 . (无法在JS控制台上生成带有"Uncaught TypeError: number is not a function"的折线 . )
JS控制台上的回溯对我来说是难以理解的,特别是由于所有.js文件都被缩小了 .
当我记录折线的路径属性和我正在添加的坐标(如下所示)时,一切似乎都有效 . 我知道coord格式正确,因为我认为Marker和Polyline应该为它们的位置采用相同的数据类型(LatLng),并且标记工作正常 . 任何人都知道发生了什么?
var mapOptions = {
center: { lat: 37.23112,
lng: -122.29398
},
zoom: 15
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// Make the line that will trace the guys route:
var polyOptions = {
strokeColor: '#000000',
srokeOpacity: 1.0,
strokeWeight: 3
};
var poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
// Make an array of everywhere the lilguys has been. Passed into this django template as {"lat": 12, "lng": 8} objects.
var journey = [{"lat": 33.2389, "lng":-123.9349}, {"lat":32.928392, "lng":-122.29289}, {"lat":33.928982, "lng":-120.298392}];
var journey_markers = [];
// Draw all the placemarks
for (var i = 0; i < journey.length; i++) {
var coord = journey[i];
journey_markers.push(new google.maps.Marker({position: coord, map:map}));
var path = poly.getPath();
console.log(coord);
console.log(path);
path.push(coord);
}
谢谢!
编辑:我将模板变量替换为他们评估的内容 . 通过查看浏览器中的HTML源代码来检查,并确认不是错误的来源 .
1 回答
想出答案 . 它似乎与Markers不同,Polyine路径需要
google.maps.LatLng()
对象而不是latlng文字 . 以下修复了此问题: