Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In addition to sudosu's fork, fixed js loading from https server. #18

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0d9138d
adding fields in lib
Feb 22, 2012
161c455
geocode the address and use that to set the lat/lng
Feb 23, 2012
64c9c74
dom methods
Feb 23, 2012
dc875be
parenthesis
Feb 23, 2012
0e7685c
trying strings instead
Feb 23, 2012
1710eaa
trying without globals?
Feb 23, 2012
fb28120
fix geocoder and bump version number
Feb 23, 2012
1307412
update readme
Feb 23, 2012
ade6edf
dom methods
Feb 23, 2012
05043ca
update readme
Feb 23, 2012
c4cb235
Merge branch 'master' of github.com:julescarbon/rails_admin_map_field
Feb 23, 2012
3c56a2e
make using country and state fields optional
vala May 10, 2013
16a352b
add map_width and map_height, and run map code in gmap load callback
vala May 10, 2013
c439dcf
Fix if address is undefined
Jun 20, 2013
461aeb9
Add back listener to watch for map clicks
Jun 20, 2013
e50904f
Add lat and lon live textfields
Jun 20, 2013
4f566a7
Update README
Jun 20, 2013
d7931ac
# This is a combination of 4 commits.
jasonl Nov 24, 2011
b53102c
Save and display data on form submission
Nov 15, 2013
2f61cbb
Save and display data on form submission
Nov 15, 2013
42f2f24
Fucked up repo - remove merge rubbish
Nov 18, 2013
797ae67
Save and display data on form submission
Nov 15, 2013
d654095
Merge branch 'master' of github.com:BartlomiejSkwira/rails_admin_map_…
Nov 18, 2013
c11dc7d
Revert to old commit - hopefully it works
Nov 22, 2013
1ab7b5a
Merge branch 'master' of github.com:BartlomiejSkwira/rails_admin_map_…
Nov 22, 2013
3ed4cee
Remove all file
Nov 22, 2013
328ad4c
fixed generation for elements ids
sudosu Feb 5, 2014
0da1884
draggable marker, delaying address update, fixed map zoom style
sudosu Feb 7, 2014
0f847be
add parameter for custom label
sudosu Feb 7, 2014
f9fc1ee
Update README.md
sudosu Feb 7, 2014
bbb55da
refactoring js
sudosu Feb 7, 2014
3efaf36
refactoring
sudosu Feb 7, 2014
2574bf2
Update README.md
sudosu Feb 7, 2014
8247ad7
Update README.md
sudosu Feb 7, 2014
f13235c
Update README.md
sudosu Feb 7, 2014
e6c1e02
Update README.md
sudosu Feb 7, 2014
e2b0fef
Update README.md
sudosu Feb 7, 2014
4725128
Update README.md
sudosu Feb 8, 2014
8872f69
Use relative protocol to load the GMaps script
tinbka Dec 8, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
Rails Admin Map Field
=====================
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(or below map), 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
=====

*New features:
- map resizing in config
- map marker draggable
- custom label for latitude field
```ruby
field :latitude, :map do
map_width 600
map_height 400
map_label "Map"
end
```

- adds :latitude, :longitude live text fields below map


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", :github => "sudosu/rails_admin_map_field"
```

Then, add in your `config/initializers/rails_admin.rb` initializer:
Expand All @@ -34,9 +49,16 @@ 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.
- `default_latitude` - the latitude to center the map shown on if the latitude field is blank. Defaults to 40.711417, the latitude of New Jersey City, NJ, USA
- `default_longitude` - the longitude used if the longitude field is blank. Defaults to 74.0647, the longitude of New Jersey City, NJ, USA.
- `map_width` - map width in px, eg: '400'
- `map_height` - map height
- `map_label` - label for map field. Defaults to "Map"


A more complicated configuration example:

Expand All @@ -59,7 +81,8 @@ LICENSE
=======
rails_admin_map_field is licensed under the MIT license.

Copyright (C) 2011 by Jason Langenauer
Copyright (C) 2011 by Jason Langenauer, Jules Laplace, Valentin
Ballestrino, Bartlomiej Skwira, Oleg Popadyuk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
91 changes: 67 additions & 24 deletions app/views/rails_admin/main/_form_map.html.haml
Original file line number Diff line number Diff line change
@@ -1,47 +1,90 @@
= javascript_include_tag ("http://maps.googleapis.com/maps/api/js?key=#{field.google_api_key}&sensor=false")

= javascript_tag do
:plain
:javascript
function initMapField() {
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 latitude = #{form.object.send(field.name) || field.default_latitude};
var longitude = #{form.object.send(field.longitude_field) || field.default_longitude};
var latlng = new google.maps.LatLng(latitude, longitude);
var geocoder = new google.maps.Geocoder();

var myOptions = {
zoom: #{field.default_zoom_level},
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};

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)}),
// Only one marker can be created
var marker = new google.maps.Marker({
draggable:true,
position: new google.maps.LatLng(latitude, longitude),
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
});
}
google.maps.event.addListener(marker,'dragend',function(e) {
updateLocation(e.latLng);
});

jQuery("##{field.address_dom_name},##{field.city_dom_name},##{field.state_dom_name}").on("keyup", function(){
clearTimeout($.data(this, 'timer'));
$(this).data('timer', setTimeout(processAddress, 1000));
});

function processAddress() {
// geocode based on this location
var address = $("##{field.address_dom_name}").val(),
city = $("##{field.city_dom_name}").val() || false,
state = $("##{field.state_dom_name}").val() || false,
address_string = "",
changed = false;

if (address.length === 0)
return;

address_string = address;
if (city) address_string += ", " + city;
if (state) address_string += ", " + state;

if ((address === undefined) || (address.length === 0))
return;

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) {
marker.setPosition(location);
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, :id => field.latitude_dom_name
= form.send :hidden_field, field.longitude_field, :id => field.longitude_dom_name
}

= javascript_include_tag "//maps.googleapis.com/maps/api/js?key=#{field.google_api_key}&sensor=false&callback=initMapField"

%style
= "##{field.dom_name} label {width: auto;display: inline;}"
= "##{field.dom_name} img {max-width: none;}"

%div.ramf-map-container{:id => field.dom_name, :style => "width:#{field.map_width}px; height:#{field.map_height}px"}
%div.control-group
= form.send :label, field.name, :class => "control-label"
%div.controls
= form.send :text_field, field.name, :id => field.latitude_dom_name

%div.control-group
= form.send :label, field.longitude_field, :class => "control-label"
%div.controls
= form.send :text_field, field.longitude_field, :id => field.longitude_dom_name
6 changes: 3 additions & 3 deletions gem.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ 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 = ["[email protected]"]
s.authors = ["Jason Langenauer","Jules Laplace"]
s.email = ["[email protected]","[email protected]"]
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"
Expand Down
67 changes: 60 additions & 7 deletions lib/rails_admin_map_field/rails_admin/config/fields/types/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,100 @@ module RailsAdmin::Config::Fields::Types
class Map < RailsAdmin::Config::Fields::Base
RailsAdmin::Config::Fields::Types::register(:map, self)

def allowed_methods
[@name, longitude_field]
end

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

register_instance_option(:map_label) do
"Map"
end

register_instance_option :label do
map_label
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
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
register_instance_option(:default_zoom_level) do
8
end

# Map heigth in px
register_instance_option(:map_width) do
300
end

# Map width in px
register_instance_option(:map_height) do
200
end

def sanitized_object_name(object_name)
object_name.gsub(/]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
end

def form_tag_id(object_name, field)
"#{sanitized_object_name(object_name.to_s)}_#{field.to_s}"
end

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

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

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

def address_dom_name
form_tag_id(bindings[:form].object_name, address_field)
end

def city_dom_name
form_tag_id(bindings[:form].object_name, city_field)
end

def state_dom_name
form_tag_id(bindings[:form].object_name, state_field)
end
end
end