【问题标题】:Pass Lat Lng var to initialize function google maps通过 Lat Lng var 初始化函数 google maps
【发布时间】:2015-09-29 00:07:30
【问题描述】:

我有一个 jQuery 脚本,它通过 Ajax 调用将邮政编码转换为纬度和经度。邮政编码是从隐藏的输入字段中检索的。那部分工作正常。 ajax 结果给了我正确的纬度/经度。

一段 HTML

<input id="zipcode" type="hidden" value="1010AK">

jQuery 中的 Ajax 调用:

jQuery(document).ready(function($){     
       var zipcode = jQuery('input#zipcode').val();    
       $.ajax({
       url: "http://maps.googleapis.com/maps/api/geocode/json?address=netherlands&components=postal_code:"+zipcode+"&sensor=false",
       method: "POST",
       success:function(data){
             latitude = data.results[0].geometry.location.lat;
             longitude= data.results[0].geometry.location.lng;
             coords = latitude+','+longitude;
             //console.log(coords);
           }    
      });   
});

在文档就绪函数之外(下方)我有一个 initialize() 函数,我希望能够将我的 coords var 传递给该函数,这样我就有了正确的纬度和经度。

初始化函数(ajax 调用后):

    function init() {
      var mapOptions = {
          zoom: 11,            
          center: new google.maps.LatLng(/* COORDS VAR */)
      };

      var mapElement = document.getElementById('my_map');
      var map = new google.maps.Map(mapElement, mapOptions);

      var image = '/wp-content/themes/example/img/foo.png';
      var myLatLng = new google.maps.LatLng(/* COORDS VAR */);
      var customMarker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: image
      });
   }
   google.maps.event.addDomListener(window, 'load', init);

我尝试了多种方法,但我摔跤的时间太长了。我基本上在想我可以像这样将 coords var 传递给 init 函数:

  • function init(coords){}
  • google.maps.event.addDomListener(window, 'load', function(){initialize(coords);});

我还尝试设置 ajax async: false,并添加了 dataType: 'json',但这并没有让我有机会传递给 init 函数。

【问题讨论】:

    标签: javascript jquery ajax google-maps


    【解决方案1】:

    您遇到的问题是,在您的 AJAX 请求返回之前,init() 已经运行(感谢添加到 window.load 的挂钩)。您需要删除该挂钩,并在 success 处理程序中手动调用 init。试试这个:

    jQuery(document).ready(function($){     
        var zipcode = jQuery('input#zipcode').val();    
        $.ajax({
            url: "http://maps.googleapis.com/maps/api/geocode/json",
            data: {
                address: 'netherlands',
                components: 'postal_code:' + zipcode,
                sensor: false
            },
            method: "POST",
            success: function(data){
                var lat = data.results[0].geometry.location.lat;
                var lng = data.results[0].geometry.location.lng;
                init(lat, lng);
            }    
        });   
    });
    
    function init(lat, lng) {
        var latlng = new google.maps.LatLng(lat, lng);
        var mapOptions = {
            zoom: 11,            
            center: latlng
        };
    
        var map = new google.maps.Map($('#my_map')[0], mapOptions);
        var customMarker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: '/wp-content/themes/decevents/img/marker_darkblue.png'
        });
    }
    

    【讨论】:

    • 谢谢 :) 很高兴为您提供帮助
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 2019-08-11
    • 2018-10-22
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多