weixin_33709219 2014-11-27 07:36 采纳率: 0%
浏览 26

Google用ajax在bpopup中映射

I use bpopup plugin for ajax popups. I need display popup with google map. Displaying popup is without problem.

js

$(document).on('click', '.aboutBranch', function(e){
  e.preventDefault();
  $.ajax({
    type: 'POST',
    url: '/ajax/show-branch',
    data: {delivery_id: ..., branch_id: ...},
    success: function(html) {
      $('#BranchBox').html(html);
      $('#BranchBox').bPopup();
    }
  });
});

In template which append ajax to #BranchBox is simple. There is stuff around google map too. I experimented with sync and async loading map. But both didn't works. Sometime map was loaded but mostly no. Timeout didn't help. Do you have any experiences with append google map to ajax loaded content (even with bpopup) ? Thanks for advice.

template

<div id="map_canvas" style="width: 100%; height: 400px;"></div>

<script type="text/javascript">
  function initialize() {
    var mapOptions = {
      center: { lat: parseFloat(latFromPhp), lng: parseFloat(lngFromPhp)},
      zoom: 15
    };

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

    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(parseFloat(latFromPhp), parseFloat(lngFromPhp)),
      title: titleFromPhp
    });

    marker.setMap(map);
  }

  //async load
  function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' + 'callback=initialize';
    document.body.appendChild(script);
  }

  window.onload = loadScript;


  //sync load (googleapis is included in main layout)
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
  • 写回答

1条回答 默认 最新

  • weixin_33726943 2014-11-27 23:32
    关注
    1. run the functions immediately, there is no need to wait for an event because the window already has been loaded when the popup opens

    2. Don't use the async loader, because it would load the API each time you open the popup(what may result in errors), furthermore it would increase the loading-time of the map inside the popup(it first waits for the ajax-response and then for the API)

    3. the maps-API calculates the size of the map-container when you create the Map-instance. When the popup doesn't already have it's final size at this moment you will miss some tiles. Trigger the resize-event of the window to load the tiles when the popup has been loaded(2nd argument of bPopup()). The API triggers the resize-event of the map in this case (where the size of the map will be re-calculated and missing tiles loaded)

      $('#BranchBox')
       .bPopup({},function(){google.maps.event.trigger(window,'resize',‌​{});}); 
      
    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中