Skip to content

Commit

Permalink
Save and display data on form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonl committed Nov 24, 2011
1 parent 1f4b4da commit abee058
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
56 changes: 44 additions & 12 deletions app/views/rails_admin/main/_form_map.html.haml
@@ -1,16 +1,48 @@
= javascript_include_tag ("http://maps.googleapis.com/maps/api/js?key=#{field.google_api_key}&sensor=false")
= #require_asset('ramf.js')

:javascript
jQuery(function(){
var latlng = new google.maps.LatLng( #{form.object.send(field.name) || field.default_latitude}, #{form.object.send(field.name) || field.default_longitude});
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("#{field.dom_name}"), myOptions);
});
= javascript_tag do
:plain
jQuery(function(){
var marker = null;
var latlng = new google.maps.LatLng(#{form.object.send(field.name) || field.default_latitude}, #{form.object.send(field.longitude_field) || field.default_longitude});

var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("#{field.dom_name}"), myOptions);

- if form.object.send(field.name) && form.object.send(field.longitude_field)
:plain
marker = new google.maps.Marker({
position: new google.maps.LatLng(#{form.object.send(field.name)},#{form.object.send(field.longitude_field)}),
map: map
});

:plain
google.maps.event.addListener(map, 'click', function(e) {
updateLocation(e.latLng);
});

function updateLocation(location) {
if(marker) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map
});
}

map.setCenter(location);
jQuery("##{field.latitude_dom_name}").val(location.lat());
jQuery("##{field.longitude_dom_name}").val(location.lng());
}

});
%div.ramf-map-container{:id => field.dom_name, :style => "width:300px;height:200px"}
= form.send :hidden_field, field.name
= form.send :hidden_field, field.longitude_field
= form.send :hidden_field, field.name, :id => field.latitude_dom_name
= form.send :hidden_field, field.longitude_field, :id => field.longitude_dom_name
18 changes: 16 additions & 2 deletions lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb
Expand Up @@ -2,6 +2,8 @@ module RailsAdmin::Config::Fields::Types
class Map < RailsAdmin::Config::Fields::Base
RailsAdmin::Config::Fields::Types::register(:map, self)

# THe name of the corresponding longitude field to match the latitude field
# in this object.
register_instance_option(:longitude_field) do
:longitude
end
Expand All @@ -10,20 +12,32 @@ class Map < RailsAdmin::Config::Fields::Base
:form_map
end

# Google Maps API Key - optional
register_instance_option(:google_api_key) do
nil
end

# 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
end

# Longitude value to display if the longitude attribute is nil
register_instance_option(:default_longitude) do
-0.5 # Longitude of London, United Kingdom
-0.126 # Longitude of London, United Kingdom
end

def dom_name
@dom_name ||= "#{bindings[:form].object_name}_#{@name}_#{@longitude_name}"
@dom_name ||= "#{bindings[:form].object_name}_#{@name}_#{@longitude_field}"
end

def latitude_dom_name
@lat_dom_name ||= "#{bindings[:form].object_name}_#{@name}"
end

def longitude_dom_name
@lon_dom_name ||= "#{bindings[:form].object_name}_#{@longitude_field}"
end
end
end

0 comments on commit abee058

Please sign in to comment.