Google Maps Add a marker to the map, then save the latitude and longitude in Ruby on Rails

I looked at the previous questions, but I can not quite match this, and so:

Basically, I have a Ruby on Rails project, and I would like to have a page where the user indicates their location on the Google map, adding a marker (only 1 should be allowed), and then store the longitude and latitude within the Ruby on Rails project I'm working on.

I would like to know what would be the best approach to this (add a map with Javascript?), But how can I get the latitude and longitude when the user clicks the button in ruby ​​on the rails?

I would really appreciate any advice / links to relevant sites, etc., since working in the ruby ​​on rails environment is quite new to me, and I'm not sure how to do it.

Thanks a lot in extended

+5
source share
5 answers

I think the first step would be to get your views to work without a map. Enter lat / long manually so that you know your views and controllers are working. Once you reach this point, take a look at the Google Maps API Documentation . Add a map to your view, figure out how to add a marker . When you add / remove a marker, you can update your lat / long inputs using JavaScript (I would use jQuery personally). At this point, you can make lat / long inputs hidden or read-only - unless you have a reason to manually update lat / long.

FYI - Google V3 API , , . V2.

+3

:

your_page.html

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=xxx;hl=en" type='text/javascript'></script>

<script type='text/javascript'>       
       var draggable_marker = null;

       $(document).ready(function() {
          if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById('map_div'));
            map.addControl(new GSmallMapControl());

            draggable_marker = new GMarker(new GLatLng(42.6976489, 23.3221545), {draggable : true,title : "Place this marker to your location");

           GEvent.addListener(draggable_marker, 'dragend', function() {  
              RubyGmap.setPosition(draggable_marker); 
           });
           GEvent.addListener(map, 'click', function(overlay, latlng, overlaylatlng){  
              RubyGmap.setMarkerPosition(draggable_marker, latlng); 
           });
          }
       });
</script>



<div id="map_div" style="width:690px;height:340px;" ></div>

ruby_gmap.js

RubyGmap = {    
setPosition: function(marker) {
    $('#latitude_field').val(marker.getLatLng().lat());
    $('#longitude_field').val(marker.getLatLng().lng());
},
setMarkerPosition: function(marker, latlng) {
    SELECTED = true;
    map.addOverlay(marker);
    marker.setLatLng(latlng);
    RubyGmap.setPosition(marker);
}
}
+4

, API Google , , . , . , . .

, .

0

API Google Rails, YM4R. API Google . README Rdoc exmample , .

, API GMaps , , Ruby. Javascript... ;)

0

Rails 3+,

it supports the seamless integration of google maps v3 into rails, http://grati.la/cartorbflw

0
source

All Articles