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',‌​{});}); 
      
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)