Jquery 如何首先执行Ajax调用以获取lat/lng,然后加载带有标记的Google地图?

Jquery 如何首先执行Ajax调用以获取lat/lng,然后加载带有标记的Google地图?,jquery,ajax,json,google-maps,latitude-longitude,Jquery,Ajax,Json,Google Maps,Latitude Longitude,我正在执行一个ajax调用来检索存储在sql数据库中的存储位置的lat和lng。这是我的ajax调用,返回所有LAT和LNG的json对象 $.ajax({ type:"GET", dataType:"json", url:"<?php echo site_url("sandbox_faizan/get_coordinates") ?>", success: function(result) {

我正在执行一个ajax调用来检索存储在sql数据库中的存储位置的lat和lng。这是我的ajax调用,返回所有LAT和LNG的json对象

$.ajax({
        type:"GET",
        dataType:"json",
        url:"<?php echo site_url("sandbox_faizan/get_coordinates") ?>",
        success: function(result)
        {
            my_arr = result;

          for(var i=0;i<result.length;i++)
           {
               store_points(result[i].lat,result[i].lng)
           }
    });
$.ajax({
键入:“获取”,
数据类型:“json”,
url:“”,
成功:功能(结果)
{
my_arr=结果;

对于(var i=0;i您有两个独立的异步事件:

  • 页面加载事件上的映射初始化
  • ajax返回标记的数据
  • 一个选项是创建一个函数,在地图初始化后显示标记,在地图初始化结束时调用它,并调用ajax成功函数,那么顺序无关紧要,当两者都发生时,标记将显示在地图上

    全局范围内的变量:

    var points = [];
    var map = null;
    
    阿贾克斯:

    将标记添加到地图:

     function addMarkers() {
        // when the map is initialized and the points have been initialized, add them to the map
        if ((map != null) && (points.length > 0)) {
           for (var i = 0; i < points.length; i++) {
              var marker = new MarkerWithLabel({
                  map: map,
                  position: points[i],
                  icon: pinImage,
                  shadow: pinShadow,
                  labelContent: count,
                  labelAnchor: new google.maps.Point(12, -5),
                  labelClass: "labels"
              });
           }
        }
     } 
    
    函数addMarkers(){
    //地图初始化且点已初始化后,将其添加到地图中
    如果((map!=null)&&(points.length>0)){
    对于(变量i=0;i
    $.ajax({
            type:"GET",
            dataType:"json",
            url:"<?php echo site_url("sandbox_faizan/get_coordinates") ?>",
            success: function(result) 
            {
              for(var i=0;i<result.length;i++) 
              {
                points.push(new google.maps.LatLng(result[i].lat,result[i].lng));
              }
              addMarkers();
            }
           });
    
        function initialize() {
            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            addMarkers();
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    
     function addMarkers() {
        // when the map is initialized and the points have been initialized, add them to the map
        if ((map != null) && (points.length > 0)) {
           for (var i = 0; i < points.length; i++) {
              var marker = new MarkerWithLabel({
                  map: map,
                  position: points[i],
                  icon: pinImage,
                  shadow: pinShadow,
                  labelContent: count,
                  labelAnchor: new google.maps.Point(12, -5),
                  labelClass: "labels"
              });
           }
        }
     }