Reputation: 6896
I built an app back in the day on rails 2.3x using geokit and y4mr-gm.
I am now building an app and Rails 3 and curious if there are any better solutions.
Basically I have a model Business that has an address. When a user searches for or browses the Businesses I want to have a map at the bottom that displays a marker for each business.
I could do this via geokit and y4mr-gm (though I hear there can be some trouble with Rails 3) but I am hoping for a more elegant solution. Is there anything recent I could use?
Thanks!
Upvotes: 3
Views: 2051
Reputation: 11055
I would say screw the plugins and just use straight up javascript from Google Maps API v3... its really not that much code and G has very accurate geocoder. Just create a js array of businesses in your view and use the some js to load the map and markers.
UNTESTED code, but it should be pretty close:
<script type="text/javascript">
addresses = [];
<% @businessses.each do |biz| %>
addresses.push('<%=biz.address%>');
<% end %>
function renderYoMap(addresses){
var gmapOptions = {
zoom: 12,
center: new google.maps.LatLng(38,-97), //near the middle of the US
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// initialize the map with your options
gmap = new google.maps.Map(document.getElementById("map_canvas"),gmapOptions);
// probably want to recenter on the first marker, so lets use a flag
recentered = false;
//iterate through your addresses
for (i in addresses) {
address = addresses[i];
geocoder = new google.maps.Geocoder();
//geocode it to get marker position
geocoder.geocode( { 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
position = new google.maps.LatLng(lat,lng);
markerOptions = {
map: gmap,
position: position
};
//create your marker on the map
new google.maps.Marker(markerOptions);
//recenter that thang if it hasnt been already
if (!recentered){
gmap.setCenter(position);
recentered = true;
}
}
}
}
renderYoMap(businesses);
</script>
Of course you can get alot fancier, but this should give you the basic functionality you are looking for. The google geocoder limit is 1000 a day, but that is client-side, so that should not be a problem.
Upvotes: 2
Reputation: 24783
I have been really happy with Geocoder. Its actively maintained and very simple. I ask for a street address and get geocoder to fill in the lat/lng fields before I save my model. It's really painless to implement.
When it comes to displaying the stuff on a map, I am still writing the JS myself, but using jQuery to do it. If you are looking for a working example of the JS with jQuery you can see mine here.
Upvotes: 4
Reputation: 7414
Well, you have several options, ruby tool box can help, but it doesn't looks up to date.
I suggest https://github.com/joshuamiller/cartographer
Cartographer generates painless Google Maps for your Rails application. It supports Google Maps API v3 & comes with all the goodies (MarkerManager && MarkerClusterer) for managing large number of markers with least effort.
have fun
Upvotes: 1
Reputation: 8757
Here's one you should check out:
https://github.com/apneadiving/Google-Maps-for-Rails
Upvotes: 1