我正在使用Gmaps4rails。它工作得很好。但是现在我在尝试通过浏览器对用户进行地理定位时遇到了问题。
我的观点:
<%= gmaps(:markers => { :data => @json},
:map_options => { :container_class => "map_container",:id => "map",:class => "gmaps4rails_map"},
:scripts => :none ) %>
<% content_for :scripts do %>
<script type="text/javascript">
Gmaps.map.callback = function() {
setInterval(function(){Gmaps.map.createMarker({
Lat: Gmaps.map.userLocation.lat(),
Lng: Gmaps.map.userLocation.lng(),
rich_marker: null,
marker_picture: "/images/icon.png",
})},10000)
}
</script>
<% end %>
我尝试了一种用于浏览器地理定位延迟的setinterval方法,如How do I display the user's location with a marker in gmaps4rails?
中所述我的gmaps咖啡脚本
detect_location: true # should the browser attempt to use geolocation detection features of HTML5?
center_on_user: true # centers map on the location detected through the browser
我的谷歌Chrome控制台出现了这个错误。
未捕获的TypeError:无法读取未定义的属性“0” Gmaps4Rails.Gmaps4Rails.createImageAnchorPositiongmaps4rails.base.js:377 Gmaps4RailsGoogle.Gmaps4RailsGoogle.createMarkergmaps4rails.googlemaps.js:127 (匿名函数)
我错过了什么?谢谢
的Stephane
答案 0 :(得分:0)
你应该这样做:
setInterval( function(){
Gmaps.map.createMarker({
Lat: Gmaps.map.userLocation.lat(),
Lng: Gmaps.map.userLocation.lng(),
rich_marker: null,
marker_picture: "https://a248.e.akamai.net/assets.github.com/images/modules/about_page/octocat.png?1315937507",
marker_width: 80,
marker_height: 80,
marker_anchor: null,
shadow_anchor: null,
shadow_picture: null,
shadow_width: null,
shadow_height: null
})},10000)
详细但不应该直接使用:)
顺便说一句,这不是一个好的实现:如果用户需要更多时间接受地理位置(或简单地拒绝),该怎么办?