简体   繁体   English

Google Maps脚本仅在我第一次打开页面时加载

[英]Google Maps script only loads the first time I open page

following this tutorial I've added a google maps component to my rails 4 project. 在完成本教程之后,我将google maps组件添加到了Rails 4项目中。 Now when I open my page in the browser (the map is on the landing/index page), the map shows, but when I use the navigation to move around, and come back the map is not there. 现在,当我在浏览器中打开页面时(地图位于着陆/索引页面上),地图显示出来,但是当我使用导航来回移动并回来时,地图不在那里。 I am new to rails and have no idea how to fix it, or why it happens. 我是Rails的新手,不知道如何解决它,或者为什么会发生。 There are no errors in the console. 控制台中没有错误。

Here is the html of the page I'm talking about: 这是我正在谈论的页面的html:

<script>
function initialize() {
var mapOptions = {
    zoom: 16,
    center: new google.maps.LatLng(46.04236, 14.48786)
  };

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

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

window.onload = loadScript;
</script>

<div class="col-xs-12 ">
    <div class="panel panel-default">
        <div class="panel-heading center">
            <h3 class="panel-title">Lokacija</h3>
        </div>
        <div class="panel-body">
            <div class="col-xs-6 center">
                <br />
                <br />
                <br/ >
                <br />
                <p>Institut Jožef Stefan</p>
                <p>Knjižnica</p>
                <p>Jamova 39</p>
                <p>1000 Ljubljana</p>
                <br />
            </div>
            <div class="col-xs-6">
            <div id="googleMap"></div>
            </div>              
        </div>
        <div class="panel-heading center">
            <h3 class="panel-title">Telefonske številke</h3>
        </div>
        <div class="panel-body">
            <div class="col-xs-6">
                <p>informacije:</p>
                <p>izposoja:</p>
                <p>bibliografije:</p>
                <p>vodja knjižnice:</p>
                <p>fax:</p>                                             
            </div>
            <div class="col-xs-6 right">
                <p>+386 1 477-3304</p>
                <p>+386 1 477-3304</p>
                <p>+386 1 477-3247</p>
                <p>+386 1 477-3258</p>
                <p>+386 1 477-3152</p>
            </div>
        </div>
        <div class="panel-heading center">
            <h3 class="panel-title">Delovni Čas</h3>
        </div>
        <div class="panel-body">
            <div class="col-xs-6">
                <p>ponedeljek-četrtek:</p>
                <p>petek:</p>
            </div>
            <div class="col-xs-6 right">
                <p>8.00-19.00</p>
                <p>8.00-17.00</p>
            </div>
        </div>
        <div class="panel-heading center">
            <h3 class="panel-title linktext"><%= link_to 'Cenik', zic_cenik_path %></h3>
        </div>

    </div>
</div>

Everything gmaps related is on here. 与gmaps相关的所有内容都在这里。 If you need more info please let me know and I'll edit the main post. 如果您需要更多信息,请告诉我,我将编辑主要帖子。 I also have a gitHub repo of the whole page. 我也有整个页面的gitHub回购。 The relevant view here would be zic/index . 此处的相关视图为zic / index。

我遇到了同样的问题,可以通过以下方法解决此问题,而不必禁用涡轮链接:

google.maps.event.addDomListener(document, 'page:change', initialize);

It sounds like the issue is that you have Turbolinks turned on, and when you navigate away and then come back, the page isn't actually receiving the load message. 听起来问题是您已打开Turbolink,并且当您导航离开然后返回时,该页面实际上没有收到加载消息。 You could either turn off Turbolinks, add the google maps script tag to the header in your application layout file, or try to reload it using the "page:changed" event. 您可以关闭Turbolinks,将google maps脚本标签添加到应用程序布局文件的标题中,或者尝试使用“ page:changed”事件重新加载它。 More information on Turbolinks can be found here: http://guides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks 可以在以下位置找到有关Turbolink的更多信息: http : //guides.rubyonrails.org/working_with_javascript_in_rails.html#turbolinks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM