我正在尝试创建一个带有方向的谷歌地图,并使用折线.但我得到上面的控制台错误,我只是无法得到这里的问题.代码:
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); }
欢迎任何帮助/建议.
错误消息是自我解释的.将label
在-Properties saved_locations
是String类型的不是,他们是数字.
您必须将值括在双引号中:
[{"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);