【问题标题】:Google Maps for Rails - Place Marker with User Location in CallbackGoogle Maps for Rails - 在回调中使用用户位置标记
【发布时间】:2012-03-30 10:16:55
【问题描述】:

我正在尝试在用户当前位置上放置一个标记。

但是,我什至无法在回调中获取用户位置。 似乎在浏览器的地理编码之前触发了console.log调用 我做错了什么?

haml 视图

= gmaps(:map_options => {:center_on_user => true})

- content_for :scripts do
  :javascript
    Gmaps.map.callback = function () {
      console.log(Gmaps.map.userLocation)
    }   

html 输出

<div class="map_container"> 
  <div id="map" class="gmaps4rails_map"></div>
</div>   


<script type="text/javascript" src="//maps.google.com/maps/api/js?v=3.5&sensor=false&amp;libraries=geometry"></script>
<script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox_packed.js"></script>
<script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.5/src/markerclusterer_packed.js"></script>
<script type="text/javascript" src="//google-maps-utility-library-v3.googlecode.com/svn/trunk/richmarker/src/richmarker-compiled.js"></script>       


<script type="text/javascript" charset="utf-8">
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.center_on_user = true;
Gmaps.map.initialize();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
window.onload = function() { Gmaps.loadMaps(); };
</script>

<script>
  //<![CDATA[
    Gmaps.map.callback = function () {
      console.log(Gmaps.map.userLocation)
    }
  //]]>
</script>  

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 gmaps4rails


    【解决方案1】:

    在这个回调的那一刻,用户位置还没有被检索到,这只是表示地图显示完成了。

    考虑一下:用户必须决定他是否接受浏览器地理定位,因此可能需要很长时间。

    地理定位成功不需要回调,我只为地理定位失败创建了一个回调。

    您应该简单地修补资产中的 js/coffee 并在 this line 调用您需要的任何函数。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    相关资源
    最近更新 更多