您好我建立实时总线跟踪Rails应用程序。我在Rails 4中使用gmaps4rails gem在Google Map上的数据库中显示了很好的标记。
控制器:
@hash = Gmaps4rails.build_markers(@vehicle_trackings) do |track, marker|
marker.lat track.latitude
marker.lng track.longitude
marker.picture({
url: "/images/Bus Filled-30.png",
width: 50,
height: 50
})
end
查看:
<div id='map' style='width: 100%; height: 500px;'></div>
<script>
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
现在如何每15秒动态更新一次标记在地图上的位置而不刷新数据库中的页面?
答案 0 :(得分:2)
<script>
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
$( document ).ready(function() {
setInterval(function(){
$(function () {
$.ajax({
type:"GET",
url:"/path_to_controller_action",
dataType:"json",
data: {some_id:1},
success:function(result){
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
handler.removeMarkers(markers);
}
markers = [];
markers = handler.addMarkers(result);
handler.bounds.extendWith(markers);
}
})
});
}, 10000);
handler.fitMapToBounds();
handler.getMap().setZoom(17);
});
});
</script>
使用适合我的ajax用x intervel替换标记。