mercredi 22 juin 2016

gmaps4rails - map breaks when user country, state, city are optional

My problem is, the user has the option to enter city, state, country. It is not mandatory for the user to enter any. I noticed that when user has entered city only or just the state only, the map breaks (shows the ocean?). I've tried to follow similar problems (not exactly the problem I'm having) but still went through them: (Gmaps4rails: How to set current user location on map by default in Rails 4) and (Fetch all entries exept those with empty column in Rails)

This is what I have so far...

userprofile.rb

geocoded_by :full_address
after_validation :geocode

def full_address
  [city, state_prov, country].compact.join(', ')
end

userprofiles_controller.rb

def index

  @userprofiles  = Userprofile.all
  @location =  @userprofiles.all(:conditions => "country <> ''")

  @hash = Gmaps4rails.build_markers(@location) do |map, marker|
    marker.lat map.latitude
    marker.lng map.longitude
    marker.picture({
                     "url" => view_context.image_path("blue_dot.png"),
                     "width" => 36,
                     "height" => 36,
                 })
  end
end

Currently, the map only works if country is there. But I need the map to work if the country, state_prov and city are all optional. So if the country is missing, but state_prov and city is filled out.. it should be able to display the marker.

Does the geocoder not return by what they can find? For example, if country is missing but state and city is there, it uses that? Any insight would help, thanks!

Aucun commentaire:

Enregistrer un commentaire