From 0d9138d849a8716ca6abf86c3adeb3d4b5237070 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 17:59:42 -0500 Subject: [PATCH 01/10] adding fields in lib --- .../rails_admin/config/fields/types/map.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb index 7a958ea..b047c42 100644 --- a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb +++ b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb @@ -8,6 +8,18 @@ class Map < RailsAdmin::Config::Fields::Base :longitude end + register_instance_option(:address_field) do + :address + end + + register_instance_option(:city_field) do + :city + end + + register_instance_option(:state_field) do + :state + end + register_instance_option(:partial) do :form_map end From 161c455ab17cac5806d7e0b5136ba13996f74a2d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 20:07:23 -0500 Subject: [PATCH 02/10] geocode the address and use that to set the lat/lng --- .../rails_admin/main/_form_map.html.haml | 30 ++++++++++++++++--- .../rails_admin/config/fields/types/map.rb | 4 +-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/views/rails_admin/main/_form_map.html.haml b/app/views/rails_admin/main/_form_map.html.haml index ef7bd3f..14892d2 100644 --- a/app/views/rails_admin/main/_form_map.html.haml +++ b/app/views/rails_admin/main/_form_map.html.haml @@ -22,9 +22,31 @@ }); :plain - google.maps.event.addListener(map, 'click', function(e) { - updateLocation(e.latLng); - }); + var old_address = ""; + jQuery("##{field.address_dom_name},##{field.city_dom_name},##{field.state_dom_name}").bind("blur", function(){ + // geocode based on this location + var address = $("##{field.address_dom_name}").val(), + city = $("##{field.city_dom_name}").val(), + state = $("##{field.state_dom_name}").val(), + address_string = "", + changed = false; + + if (address.length === 0 || city.length === 0 || state.length === 0) + return; + + address_string = [address + ",", city, self.get("state"), self.get("zip")].join(" "); + if (address_string === old_address) + return; + + old_address = address_string; + + Science.MapView.geocoder.geocode({ 'address': address_string }, function(results, status){ + if (! results || results.length === 0 || status !== "OK") return; + + var location = results[0].geometry.location; + updateLocation(location) + }); + } function updateLocation(location) { if(marker) { @@ -44,4 +66,4 @@ }); %div.ramf-map-container{:id => field.dom_name, :style => "width:300px;height:200px"} = form.send :hidden_field, field.name, :id => field.latitude_dom_name -= form.send :hidden_field, field.longitude_field, :id => field.longitude_dom_name \ No newline at end of file += form.send :hidden_field, field.longitude_field, :id => field.longitude_dom_name diff --git a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb index b047c42..b46eb90 100644 --- a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb +++ b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb @@ -32,12 +32,12 @@ class Map < RailsAdmin::Config::Fields::Base # Latitude value to display in the map if the latitude attribute is nil # (Otherwise the location defaults to (0,0) which is in the Gulf of Guinea register_instance_option(:default_latitude) do - 51.5 # Latitude of London, United Kingdom + 40.711417 # Latitude of Jersey City, NJ end # Longitude value to display if the longitude attribute is nil register_instance_option(:default_longitude) do - -0.126 # Longitude of London, United Kingdom + 74.0647 # Longitude of Jersey City, NJ end # Default zoom level of the map From 64c9c746f7c6d245268c7d33436190a4704b93ef Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 20:20:39 -0500 Subject: [PATCH 03/10] dom methods --- .../rails_admin/config/fields/types/map.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb index b46eb90..78c706d 100644 --- a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb +++ b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb @@ -56,5 +56,17 @@ def latitude_dom_name def longitude_dom_name @lon_dom_name ||= "#{bindings[:form].object_name}_#{@longitude_field}" end + + def address_dom_name + @address_dom_name ||= "#{bindings[:form].object_name}_#{@address_field}" + end + + def city_dom_name + @city_dom_name ||= "#{bindings[:form].object_name}_#{@city_field}" + end + + def state_dom_name + @state_dom_name ||= "#{bindings[:form].object_name}_#{@state_field}" + end end end From dc875be4ae2fa8f0fa8f71aaf6c5c3e8fc2d2329 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 20:23:54 -0500 Subject: [PATCH 04/10] parenthesis --- app/views/rails_admin/main/_form_map.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/rails_admin/main/_form_map.html.haml b/app/views/rails_admin/main/_form_map.html.haml index 14892d2..2eca0a6 100644 --- a/app/views/rails_admin/main/_form_map.html.haml +++ b/app/views/rails_admin/main/_form_map.html.haml @@ -46,7 +46,7 @@ var location = results[0].geometry.location; updateLocation(location) }); - } + }); function updateLocation(location) { if(marker) { From 0e7685c47e1c43e577e9ef11c5768f7e7beb46d6 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 21:29:32 -0500 Subject: [PATCH 05/10] trying strings instead --- app/views/rails_admin/main/_form_map.html.haml | 2 +- .../rails_admin/config/fields/types/map.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/views/rails_admin/main/_form_map.html.haml b/app/views/rails_admin/main/_form_map.html.haml index 2eca0a6..cb438cd 100644 --- a/app/views/rails_admin/main/_form_map.html.haml +++ b/app/views/rails_admin/main/_form_map.html.haml @@ -34,7 +34,7 @@ if (address.length === 0 || city.length === 0 || state.length === 0) return; - address_string = [address + ",", city, self.get("state"), self.get("zip")].join(" "); + address_string = [address, city, state].join(", "); if (address_string === old_address) return; diff --git a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb index 78c706d..a93d169 100644 --- a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb +++ b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb @@ -5,19 +5,19 @@ class Map < RailsAdmin::Config::Fields::Base # THe name of the corresponding longitude field to match the latitude field # in this object. register_instance_option(:longitude_field) do - :longitude + "longitude" end register_instance_option(:address_field) do - :address + "address" end register_instance_option(:city_field) do - :city + "city" end register_instance_option(:state_field) do - :state + "state" end register_instance_option(:partial) do From 1710eaa13804ca34e593db78a48c3dda4fc0acac Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 21:35:28 -0500 Subject: [PATCH 06/10] trying without globals? --- .../rails_admin/config/fields/types/map.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb index a93d169..68a31c8 100644 --- a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb +++ b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb @@ -46,27 +46,27 @@ class Map < RailsAdmin::Config::Fields::Base end def dom_name - @dom_name ||= "#{bindings[:form].object_name}_#{@name}_#{@longitude_field}" + "#{bindings[:form].object_name}_#{@name}_#{longitude_field}" end def latitude_dom_name - @lat_dom_name ||= "#{bindings[:form].object_name}_#{@name}" + "#{bindings[:form].object_name}_#{@name}" end def longitude_dom_name - @lon_dom_name ||= "#{bindings[:form].object_name}_#{@longitude_field}" + "#{bindings[:form].object_name}_#{longitude_field}" end def address_dom_name - @address_dom_name ||= "#{bindings[:form].object_name}_#{@address_field}" + "#{bindings[:form].object_name}_#{address_field}" end def city_dom_name - @city_dom_name ||= "#{bindings[:form].object_name}_#{@city_field}" + "#{bindings[:form].object_name}_#{city_field}" end def state_dom_name - @state_dom_name ||= "#{bindings[:form].object_name}_#{@state_field}" + "#{bindings[:form].object_name}_#{state_field}" end end end From fb281208a171e655d1213cac73886b7ac34c52c7 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 21:40:08 -0500 Subject: [PATCH 07/10] fix geocoder and bump version number --- app/views/rails_admin/main/_form_map.html.haml | 4 +++- gem.gemspec | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/views/rails_admin/main/_form_map.html.haml b/app/views/rails_admin/main/_form_map.html.haml index cb438cd..f1f9fdc 100644 --- a/app/views/rails_admin/main/_form_map.html.haml +++ b/app/views/rails_admin/main/_form_map.html.haml @@ -13,6 +13,8 @@ }; var map = new google.maps.Map(document.getElementById("#{field.dom_name}"), myOptions); + var geocoder = new google.maps.Geocoder(); + - if form.object.send(field.name) && form.object.send(field.longitude_field) :plain @@ -40,7 +42,7 @@ old_address = address_string; - Science.MapView.geocoder.geocode({ 'address': address_string }, function(results, status){ + geocoder.geocode({ 'address': address_string }, function(results, status){ if (! results || results.length === 0 || status !== "OK") return; var location = results[0].geometry.location; diff --git a/gem.gemspec b/gem.gemspec index a58f03b..cb1188d 100644 --- a/gem.gemspec +++ b/gem.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.name = "rails_admin_map_field" - s.version = "0.0.1" + s.version = "0.0.2" s.platform = Gem::Platform::RUBY s.authors = ["Jason Langenauer"] s.email = ["jason@jasonlangenauer.com"] From 13074121d479ee3b1a5507d44d2e6f067d8413ae Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 22:00:23 -0500 Subject: [PATCH 08/10] update readme --- README.md | 7 +++++-- gem.gemspec | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a6c43d6..a8e57d5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Rails Admin Map Field rails_admin_map_field is a gem that works with sferik's **rails_admin** (https://github.com/sferik/rails_admin) to provide an easy to use Google Maps interface for displaying and setting geographic co-ordinates in a model. -Where a latitude and longitude is set on the model, it is indicated by a marker shown on a Google map centered at the marker. The administrator can change the value of these fields by clicking on the desired new location on the map. +Where a latitude and longitude is set on the model, it is indicated by a marker shown on a Google map centered at the marker. The administrator is expected to also store address, city, and state fields. As these fields are filled in, the Maps API is used to geocode the address in order to determine the latitude and longitude of the map marker, which is updated on the fly. Usage ===== @@ -11,7 +11,7 @@ Usage rails_admin_map_field expects that the model will have two attributes, one for latitude and one for longitude of the point represented. To enable rails_admin_map_field, add the following to your `Gemfile`: ```ruby -gem "rails_admin_map_field", :git => "git://github.com/jasonl/rails_admin_map_field.git" +gem "rails_admin_map_field", :git => "git://github.com/trademobile/rails_admin_map_field.git" ``` Then, add in your `config/initializers/rails_admin.rb` initializer: @@ -34,6 +34,9 @@ Configuration For different configurations, rails_admin_map_field can be configured with the following: - `longitude_field` - the name of the longitude field that forms the the co-ordinate with the latitude field specified. Defaults to "longitude" +- `address_field` - the name of the address field. Defaults to "address" +- `city_field` - the name of the city field. Defaults to "city" +- `state_field` - the name of the state field. Defaults to "state" - `google_api_key` - if you use a Google Maps API Key, it can be specified here. - `default_latitude` - the latitude to center the map shown on if the latitude field is blank. Defaults to 51.5, the latitude of London, UK. - `default_longitude` - the longitude used if the longitude field is blank. Defaults to -0.126, the longitude of London, UK. diff --git a/gem.gemspec b/gem.gemspec index cb1188d..95a079c 100644 --- a/gem.gemspec +++ b/gem.gemspec @@ -16,8 +16,8 @@ Gem::Specification.new do |s| s.name = "rails_admin_map_field" s.version = "0.0.2" s.platform = Gem::Platform::RUBY - s.authors = ["Jason Langenauer"] - s.email = ["jason@jasonlangenauer.com"] + s.authors = ["Jason Langenauer","Jules Laplace"] + s.email = ["jason@jasonlangenauer.com","jules@okfoc.us"] s.homepage = "http://github.com/jasonl/" s.summary = "Adds a map field using the Google Maps API to rails_admin" s.description = "A map field for RailsAdmin that can be used to manipulate a latitude/longitude field pair" From ade6edfb198cf81049704cf8a1d5e34a6282ac1c Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 20:20:39 -0500 Subject: [PATCH 09/10] dom methods parenthesis trying strings instead trying without globals? fix geocoder and bump version number --- .../rails_admin/main/_form_map.html.haml | 8 +++--- gem.gemspec | 2 +- .../rails_admin/config/fields/types/map.rb | 26 ++++++++++++++----- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/views/rails_admin/main/_form_map.html.haml b/app/views/rails_admin/main/_form_map.html.haml index 14892d2..f1f9fdc 100644 --- a/app/views/rails_admin/main/_form_map.html.haml +++ b/app/views/rails_admin/main/_form_map.html.haml @@ -13,6 +13,8 @@ }; var map = new google.maps.Map(document.getElementById("#{field.dom_name}"), myOptions); + var geocoder = new google.maps.Geocoder(); + - if form.object.send(field.name) && form.object.send(field.longitude_field) :plain @@ -34,19 +36,19 @@ if (address.length === 0 || city.length === 0 || state.length === 0) return; - address_string = [address + ",", city, self.get("state"), self.get("zip")].join(" "); + address_string = [address, city, state].join(", "); if (address_string === old_address) return; old_address = address_string; - Science.MapView.geocoder.geocode({ 'address': address_string }, function(results, status){ + geocoder.geocode({ 'address': address_string }, function(results, status){ if (! results || results.length === 0 || status !== "OK") return; var location = results[0].geometry.location; updateLocation(location) }); - } + }); function updateLocation(location) { if(marker) { diff --git a/gem.gemspec b/gem.gemspec index a58f03b..cb1188d 100644 --- a/gem.gemspec +++ b/gem.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.name = "rails_admin_map_field" - s.version = "0.0.1" + s.version = "0.0.2" s.platform = Gem::Platform::RUBY s.authors = ["Jason Langenauer"] s.email = ["jason@jasonlangenauer.com"] diff --git a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb index b46eb90..68a31c8 100644 --- a/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb +++ b/lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb @@ -5,19 +5,19 @@ class Map < RailsAdmin::Config::Fields::Base # THe name of the corresponding longitude field to match the latitude field # in this object. register_instance_option(:longitude_field) do - :longitude + "longitude" end register_instance_option(:address_field) do - :address + "address" end register_instance_option(:city_field) do - :city + "city" end register_instance_option(:state_field) do - :state + "state" end register_instance_option(:partial) do @@ -46,15 +46,27 @@ class Map < RailsAdmin::Config::Fields::Base end def dom_name - @dom_name ||= "#{bindings[:form].object_name}_#{@name}_#{@longitude_field}" + "#{bindings[:form].object_name}_#{@name}_#{longitude_field}" end def latitude_dom_name - @lat_dom_name ||= "#{bindings[:form].object_name}_#{@name}" + "#{bindings[:form].object_name}_#{@name}" end def longitude_dom_name - @lon_dom_name ||= "#{bindings[:form].object_name}_#{@longitude_field}" + "#{bindings[:form].object_name}_#{longitude_field}" + end + + def address_dom_name + "#{bindings[:form].object_name}_#{address_field}" + end + + def city_dom_name + "#{bindings[:form].object_name}_#{city_field}" + end + + def state_dom_name + "#{bindings[:form].object_name}_#{state_field}" end end end From 05043ca11d1e9448b9b10f71b186d23d05dbdc71 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 22 Feb 2012 22:00:23 -0500 Subject: [PATCH 10/10] update readme updating readme --- README.md | 9 ++++++--- gem.gemspec | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a6c43d6..14380d2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Rails Admin Map Field rails_admin_map_field is a gem that works with sferik's **rails_admin** (https://github.com/sferik/rails_admin) to provide an easy to use Google Maps interface for displaying and setting geographic co-ordinates in a model. -Where a latitude and longitude is set on the model, it is indicated by a marker shown on a Google map centered at the marker. The administrator can change the value of these fields by clicking on the desired new location on the map. +Where a latitude and longitude is set on the model, it is indicated by a marker shown on a Google map centered at the marker. The administrator is expected to also store address, city, and state fields. As these fields are filled in, the Maps API is used to geocode the address in order to determine the latitude and longitude of the map marker, which is updated on the fly. Usage ===== @@ -11,7 +11,7 @@ Usage rails_admin_map_field expects that the model will have two attributes, one for latitude and one for longitude of the point represented. To enable rails_admin_map_field, add the following to your `Gemfile`: ```ruby -gem "rails_admin_map_field", :git => "git://github.com/jasonl/rails_admin_map_field.git" +gem "rails_admin_map_field", :git => "git://github.com/trademobile/rails_admin_map_field.git" ``` Then, add in your `config/initializers/rails_admin.rb` initializer: @@ -34,6 +34,9 @@ Configuration For different configurations, rails_admin_map_field can be configured with the following: - `longitude_field` - the name of the longitude field that forms the the co-ordinate with the latitude field specified. Defaults to "longitude" +- `address_field` - the name of the address field. Defaults to "address" +- `city_field` - the name of the city field. Defaults to "city" +- `state_field` - the name of the state field. Defaults to "state" - `google_api_key` - if you use a Google Maps API Key, it can be specified here. - `default_latitude` - the latitude to center the map shown on if the latitude field is blank. Defaults to 51.5, the latitude of London, UK. - `default_longitude` - the longitude used if the longitude field is blank. Defaults to -0.126, the longitude of London, UK. @@ -59,7 +62,7 @@ LICENSE ======= rails_admin_map_field is licensed under the MIT license. -Copyright (C) 2011 by Jason Langenauer +Copyright (C) 2011 by Jason Langenauer and Jules Laplace Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/gem.gemspec b/gem.gemspec index cb1188d..95a079c 100644 --- a/gem.gemspec +++ b/gem.gemspec @@ -16,8 +16,8 @@ Gem::Specification.new do |s| s.name = "rails_admin_map_field" s.version = "0.0.2" s.platform = Gem::Platform::RUBY - s.authors = ["Jason Langenauer"] - s.email = ["jason@jasonlangenauer.com"] + s.authors = ["Jason Langenauer","Jules Laplace"] + s.email = ["jason@jasonlangenauer.com","jules@okfoc.us"] s.homepage = "http://github.com/jasonl/" s.summary = "Adds a map field using the Google Maps API to rails_admin" s.description = "A map field for RailsAdmin that can be used to manipulate a latitude/longitude field pair"