javascript - 谷歌地图仅在 "refresh"之后加载

标签 javascript ruby-on-rails google-maps coffeescript

我的 Rails 4 应用程序运行 CoffeeScript 来绘制 Google map 。乍一看,它似乎不起作用。然而,当我点击浏览器刷新按钮时, map 加载得非常顺利。这已经过测试并且正在发生:

  • 移动版 Safari (iOS7)
  • 桌面版 Safari (OSX)
  • Chrome (OSX)
  • Chrome(Windows 7)

来自 application.html.erb 的 header

<head>
    <!-- Boilerplate -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <%= stylesheet_link_tag "normalize.min.css" %>
    <%= stylesheet_link_tag "main.css" %>
    <%= javascript_include_tag "vendor/modernizr-2.6.2-respond-1.1.0.min.js" %>

    <!-- Icon Font -->
    <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">

    <!-- Google Maps API -->
    <%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?key=*********&sensor=true" %>

    <!-- Rails -->
    <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>

    <link href='http://fonts.googleapis.com/css?family=Droid+Sans|Lobster' rel='stylesheet' type='text/css'>
</head>

application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require_tree .

locations.js.coffee

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

initialize = ->
    mapOptions =
        center: new google.maps.LatLng(33.51976, -101.95781)
        zoom: 16
        mapTypeId: google.maps.MapTypeId.ROADMAP

    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)

    myLatlng = new google.maps.LatLng(33.51976, -101.95781);
    marker = new google.maps.Marker(
        position: myLatlng
        map: map
        title: "Hello World!"
    )

    contentString = "<div id=\"info_content\">" + "<h3>Mighty Wash Amazing Autobath</h3>" + "<ul>" + "<li>6506 82nd Street, Lubbock, Texas 79424</li>" + "<li>806.553.0722</li>" + "<li>8:00am - 9:00pm</li>" + "</ul>" + "</div>"r
    infowindow = new google.maps.InfoWindow(content: contentString)

    google.maps.event.addListener marker, "click", ->
        infowindow.open map, marker

    infowindow.open(map, marker)

google.maps.event.addDomListener window, "load", initialize

locations/index.html.erb

<div id="map-canvas"/>

最佳答案

这听起来很像 Rails javascript only works after reload .建议的解决方案是用以下内容包装您的 coffeescript:

ready = ->
// functions

$(document).ready(ready)
$(document).on('page:load', ready)

关于javascript - 谷歌地图仅在 "refresh"之后加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19185403/

相关文章:

javascript - 如何在 HH :MM AM format in Ionic 中显示日期时间

javascript - 客户端应用程序的模块化方法

javascript - 在 jquery 中返回 span 的空值?

javascript - 如何在打开新窗口后聚焦主网页?

ruby-on-rails - 在 ubuntu 启动时运行 rake 任务

ruby-on-rails - 如何按 created_at 属性对集合进行排序

iphone - 从 google places API 获取数据后获取状态 "REQUEST_DENIED"

ruby-on-rails - rails 3 before_update 回调

iphone - 使用适用于 iOS 的新 GoogleMaps SDK 找不到文件

android - 二进制 XML 文件行 #1 : Error inflating class fragment MapFragment