gmaps4rails detect_location未显示标记

时间:2013-06-18 10:00:08

标签: ruby-on-rails-3 gmaps4rails

我在rails 3.2.12中使用gem“gmaps4rails”,我在map_options中设置了detect_location =“true”,但它没有显示任何带有用户当前位置的标记。

这是代码:

<%= gmaps("map_options" => {"auto_adjust" => true, "detect_location" => true, "center_on_user" => true, "auto_zoom" => false, "radius" => 2500},
  "markers" => {"data" => @json, "options" => {"list_container" => "markers_list","randomize" => true,"max_random_distance" => 10000, "draggable" => true, "dblclick" => "latLng"} }

如何使用标记显示用户当前位置。

请帮帮我。 在此先感谢

1 个答案:

答案 0 :(得分:0)

使用宝石“gmaps4rails”,

如果我们提供选项"detect_location" => true,当地图加载到页面上时,它会向用户显示弹出窗口“允许应用程序使用您的位置”,并将“是”和“否”作为选项。

如果用户选择“是”,则将地图集中到用户的位置。

现在,您要显示用户位置的标记,然后您必须自己添加标记。

这可以按如下方式完成:

控制器:

geo = request.location
@latitude  = geo.latitude
@longitude = geo.longitude

在页面加载后,您可以使用此回调为用户添加标记。

这将在地图加载页面时为当前用户位置添加标记。

- content_for :scripts do
  %script{:type => "text/javascript"}
    Gmaps.map.callback = function(){
    google.maps.event.addListenerOnce(Gmaps.map.serviceObject, 'idle', function(){
    / add user pin
    var home_pin = [{picture: "current_user.png", width: 32, lat: "#{@latitude}", lng: "#{@longitude}"}];
    Gmaps.map.addMarkers(home_pin)
    }
    )};

希望这会对你有所帮助。

如果您仍然遇到任何问题,请告诉我。

感谢。