我需要从回调函数中获取
JSON对象数据,以便我可以在页面中稍后处理它,而不是像我现在那样在回调中处理它.对其他人来说一定是显而易见的,因为我看不到任何关于它的文章.谁能告诉我怎么做?
这是我的代码:
<script type="text/javascript" src="/site_media/js/jstree/_lib/jquery.js"></script> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> function initialize() { var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var myOptions = { zoom: 4,center: myLatlng,mapTypeId: google.maps.MapTypeId.ROADMAP,} map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); return map; } function add_marker(map,lat,long) { var marker_image='/site_media/images/map_marker.png'; var myLatlng = new google.maps.LatLng(lat,long); title='title'; var marker = new google.maps.Marker({ position: myLatlng,map: map,title:title,icon:marker_image }); //map.panTo(myLatlng); } //window.onload=initialize(); setTimeout('map=initialize();',2000); $.getJSON("/ajax/get",function(data) { $.each(data,function(i,val) { latitude = val.fields.latitude; longitude = val.fields.longitude; add_marker(map,latitude,longitude); }); }); </script> <div id="map_canvas" style="width: 500px; height: 300px"></div>
解决方法
Rich,它实际上比你想象的要简单得多(至少看起来那样).我假设你的标记有id或者什么.您可能需要调整此项才能按照您的要求工作:
var lastMarkerId; // We'll store the id here,starts as undefined function refresh_markers () { $.getJSON("/ajax/get",{ marker_id: lastMarkerId },function(data) { $.each(data,val) { latitude = val.fields.latitude; longitude = val.fields.longitude; add_marker(map,longitude); }); if (data.length) { // grab the last item and store its ID lastMarkerId = data.pop().id; } }); }
然后在您的服务器上执行以下操作:“如果marker_id有值,请在该ID后找到每个标记,否则,将它们全部返回”.
[{id:1,fields: {latitude: "..",longitude: ".." }}]