【发布时间】:2014-02-02 01:10:57
【问题描述】:
我正在尝试根据 AJAX 调用返回的数据每 x 秒更新一次谷歌地图上的标记。每 x 秒调用一次 Ajax 函数,但不显示标记。下面是我写的 JavaScript。有人知道原因吗?谢谢。
<script type="text/javascript">
var map
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(1.32643, 103.79426),
zoom: 11
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
//Ajax call to get position
function set_center() {
var feedback = $.ajax({
type: 'GET',
url: 'get_gps_position',
success: function (data) {
console.log(data);
if (data['gps_position_longitude'] != null && data['gps_position_latitude'] != null ) {
var latlng = new google.maps.LatLng(data['gps_position_longitude'], data['gps_position_latitude']);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Hello World!"});
};
},
error: function(data) {
$("#result").html(data);
console.log(data)
}
});
}
setInterval(set_center, 10000);
</script>
【问题讨论】:
标签: javascript ajax google-maps google-maps-api-3