我正在尝试使用折线创建一个带有路线的谷歌地图。但我收到上述控制台错误,我只是无法解决这里的问题。 代码:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
var marker;
var infowindow;
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var labelIndex = 0;
var markers = {};
var currentId = 0;
var uniqueId = function() {
return ++currentId;
}
var edit = 0;
var direction = '';
var linecordinates = [];
var map;
var sortorder = 0;
var latest_marker;
var latest_latlng;
var lineSymbol;
var flightPath;
var line = [];
var saved_locations = $("#hidden_lat_long").val();
// saved_locations = [{"label":1,"lat":"27.7233","long":"85.2783"},{"label":2,"lat":"27.7625","long":"85.3411"},{"label":3,"lat":"27.7056","long":"85.4166"},{"label":4,"lat":"27.6704","long":"85.3209"}]
function initialize() {
lineSymbol = {
path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW
};
/* edit portion */
if(saved_locations != '') {
edit = 1;
saved_locations = JSON.parse(saved_locations);
currentId = labelIndex = sortorder = saved_locations.length;
}
/* edit portion */
var latlng = new google.maps.LatLng(27.7238,85.3214);
var options = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map-canvas"), options);
/* edit portion */
if(edit) {
for( i = 0; i < saved_locations.length; i++ ) {
var position = new google.maps.LatLng(saved_locations[i].lat, saved_locations[i].long);
var id = saved_locations[i].label;
var saved_label = saved_locations[i].label;
marker = new google.maps.Marker({
id: id,
position: position,
map: map,
label: saved_label,
});
console.log(marker);
markers[id] = marker;
linecordinates.push({
id: id,
lat: parseFloat(saved_locations[i].lat),
lng: parseFloat(saved_locations[i].long)
});
}
flightPath = new google.maps.Polyline({
path: linecordinates,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
icons: [{
icon: lineSymbol,
offset: '100%'
}],
});
addline();
}
/* edit portion */
}
google.maps.event.addDomListener(window, 'load', initialize);
function addline() {
line.push(flightPath);
flightPath.setMap(map);
}
<div id="map-canvas"></div>
欢迎任何帮助/建议。
答案 0 :(得分:5)
错误信息是自我解释的。
label
中的saved_locations
- 属性不是String类型,它们是Numbers。
您必须将值括在双引号中:
[{"label":"1","lat":"27.7233","long":"85.2783"},{"label":"2","lat":"27.7625","long":"85.3411"},{"label":"3","lat":"27.7056","long":"85.4166"},{"label":"4","lat":"27.6704","long":"85.3209"}]
...或转换它们:
var saved_label = String(saved_locations[i].label);