我无法在我的问题上找到正确的答案我需要帮助我尝试从mysql制作动态标记并在地图上显示它当前我从数据库中使用了latIn。我将数据从mysql传递给函数initMap参数并尝试循环它,以便它在地图上动态显示标记。
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCysW8SYjBdZlZa91kpUCV3VQBEm3EQGgE&callback=initMap" type="text/javascript"></script>
$.ajax({
url: "Gmap.aspx/GetLocationLatIn",
data: "",
dataType: "json",
type: "POST",
contentType: "application/json; chartset=utf-8",
success: function (data) {
locationLatIn = JSON.parse(JSON.stringify(data.d));
var holder = [];
var allLocations = "";
for (var i = 0; i < $(locationLatIn).toArray().length; i++)
{
holder.push(locationLatIn[i].locationLatInData.toString());
}
initMap(holder);
}
});
})
function initMap(holder) {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: new google.maps.LatLng(14.6362904, 121.027754),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < holder.length; i++) { //Im getting an Error here saying that holder is undefined so the property length cant be read.
marker = new google.maps.Marker({
position: new google.maps.LatLng(holder, 121.027754),
map: map
});
google.maps.event.addListener(marker, 'hover', (function
(marker, i) {
return function () {
infowindow.setContent('Name');
infowindow.open(map, marker);
}
})(marker, i));
}
答案 0 :(得分:0)
$(locationLatIn).toArray().length
- 此时可能你会收到错误。
在控制台中检查$(locationLatIn)
另外,为什么要对data.id进行stringyfying和解析。
它会返回元素标记或元素ID等。您尝试访问的内容是什么。