我正在尝试搜索 10 英里内的用户地址。只要距离在 10 英里以内,它似乎就可以正常工作。但是,如果它超过 10 英里,它会放大到偏僻的地方。任何想法如何修复它,以便如果它不在范围内它不会缩放到随机海洋,甚至更好地给出一条消息说在给定半径内不匹配?
users_controller
class UsersController < ApplicationController
def index
if params[:search].present?
@users = User.near(params[:search], 10)
else
@users = User.all
end
@hash = Gmaps4rails.build_markers(@users) do |user, marker|
marker.lat user.latitude
marker.lng user.longitude
marker.infowindow user.description
end
end
end
index.html.erb
<div id="map-container">
<div id="map-canvas"></div>
</div>
<%= form_tag users_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search Near" %>
</p>
<% end %>
<script>
$(document).ready(function(){
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map-canvas'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>)
handler.bounds.extendWith(markers);
handler.fitMapToBounds(markers);
if(navigator.geolocation)
navigator.geolocation.getCurrentPosition;
});
});
</script>