Module: Gmaps4rails::ActsAsGmappable::InstanceMethods

Defined in:
lib/gmaps4rails/acts_as_gmappable.rb

Instance Method Summary collapse

Instance Method Details

#process_geocodingObject

This is a before_filter to trigger the geocoding and save its results



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gmaps4rails/acts_as_gmappable.rb', line 13

def process_geocoding
  #to prevent geocoding each time a save is made
  return true if gmaps4rails_options[:check_process] == true && self.send(gmaps4rails_options[:checker]) == true
  #try to geocode
  begin
    coordinates = Gmaps4rails.geocode(self.send(gmaps4rails_options[:address]), gmaps4rails_options[:language], false, gmaps4rails_options[:protocol])
  rescue GeocodeStatus, GeocodeInvalidQuery => e  #address was invalid, add error to address.
    Rails.logger.warn(e)
    errors[gmaps4rails_options[:address]] << gmaps4rails_options[:msg] if Gmaps4rails.condition_eval(self, gmaps4rails_options[:validation])
  rescue GeocodeNetStatus => e #connection error, No need to prevent save.
    Rails.logger.warn(e)
    #TODO add customization here?
  else #if no exception, save the values
    self.send(gmaps4rails_options[:lng_column]+"=", coordinates.first[:lng]) if self.respond_to?(gmaps4rails_options[:lng_column]+"=")
    self.send(gmaps4rails_options[:lat_column]+"=", coordinates.first[:lat]) if self.respond_to?(gmaps4rails_options[:lat_column]+"=")
    unless gmaps4rails_options[:normalized_address].nil?
      self.send(gmaps4rails_options[:normalized_address].to_s+"=", coordinates.first[:matched_address])
    end
    # Call the callback method to let the user do what he wants with the data
    self.send(gmaps4rails_options[:callback], coordinates.first[:full_data]) unless gmaps4rails_options[:callback].nil?
    if Gmaps4rails.condition_eval(self, gmaps4rails_options[:check_process])
      self[gmaps4rails_options[:checker]] = true
    end
  end
end

#to_gmaps4rails(&block) ⇒ Object



39
40
41
42
43
# File 'lib/gmaps4rails/acts_as_gmappable.rb', line 39

def to_gmaps4rails(&block)
  json = "["
  json << Gmaps4rails.create_json(self, &block) #removes the extra comma
  json << "]"
end