Gmaps4rails deaults to ocean view when match not found

80 views Asked by At

I'm trying to search for user address within 10 miles. It seems to work fine as long as distance is within 10 miles. But, if it is more than 10 miles it zooms to middle of nowhere. Any idea how to fix it so that if it is not within range it doesn't zoom to random ocean, even better give a message saying no match in given radius?

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>
0

There are 0 answers