gmaps4rails根据当前用户的位置放大

时间:2014-05-30 16:35:46

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

我使用带有rails 4.1的新gmaps4rails我已成功地在地图上显示所有标记,尽管地图缩小以向所有标记显示整个世界。我希望地图开始在current_user的某个半径内放大。我该怎么办呢? 谢谢

我的观点:

<div style='width: 100%;'>
  <div id="big_map" style='width: 100%; height: 500px;' data-lat= "<%= current_user.latitude %>" data-lng= "<%=current_user.longitude %>"></div>
</div>

<script type="text/javascript">
    handler = Gmaps.build('Google');
    handler.buildMap({ provider: {}, internal: {id: 'big_map'}}, function(){
      markers = handler.addMarkers(<%= raw(@hash.to_json) %>);
      handler.bounds.extendWith(markers);
      handler.fitMapToBounds();
    });
</script>

我的行动:

  def map
    if user_signed_in?
      @users = User.where('latitude IS NOT NULL').all
      @hash = Gmaps4rails.build_markers(@users) do |user, marker|
      marker.lat user.latitude
      marker.lng user.longitude
    end
    else
      redirect_to new_user_registration_url
    end
  end

1 个答案:

答案 0 :(得分:1)

var currentUserPosition = <%=raw [ current_user.latitude, current_user.longitude].to_json %>;
var handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'big_map'}}, function(){
  var markers = handler.addMarkers(<%= raw(@hash.to_json) %>);
  handler.map.centerOn(currentUserPosition);

  // if you want to set the zoom level
  // handler.getMap().setZoom(10)

  // if you want to adapt on current user, no need to do this
  // handler.bounds.extendWith(markers);
  // handler.fitMapToBounds();
});