简体   繁体   English

谷歌地图添加标记到地图,然后在Ruby on Rails中存储纬度和经度

[英]Google Maps Add a Marker to Map, then Store Latitude and Longitude in Ruby on Rails

I've had a look at the previous questions asked but i cannot quite seem to fit this in so here it is: 我已经看了之前提出的问题,但我似乎不太适合这样,所以这里是:

Basically, I have a Ruby on Rails project and i would like to have a page where the user pin points his location on a google map by adding a marker (only 1 should be permitted) and then store the longitude and latitude within the Ruby on Rails project i am working on. 基本上,我有一个Ruby on Rails项目,我希望有一个页面,用户可以通过添加标记(只允许1个)来确定谷歌地图上的位置,然后将经度和纬度存储在Ruby中我正在研究的Rails项目。

I would like to know what would be the best approach to this (add map with Javascript?) but then how would i retrieve the latitude and longitude when the user hits a button within ruby on rails? 我想知道最好的方法是什么(用Javascript添加地图?)但是当用户点击轨道上的ruby内的按钮时,我将如何检索纬度和经度?

I would really appreciate any tips / links to relevant sites etc, as working in a ruby on rails environment is pretty new to me and i'm not sure how to go about doing the above. 我真的很感激任何提示/链接到相关网站等,因为在铁轨环境中工作ruby对我来说是一个新的,我不知道如何去做上面的事情。

Thanks a lot in advanced 非常感谢先进

Here is a short example: 这是一个简短的例子:

your_page.html 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 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);
}
}

I think the first step would be to get your views working without a map. 我认为第一步是让您的视图在没有地图的情况下工作。 Enter lat/long manually so you know your views and controllers are working. 手动输入lat / long,以便了解您的视图和控制器是否正常工作。 Once you reach that point, take a look at the Google Maps API documentation . 达到这一点后,请查看Google Maps API文档 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 with JavaScript (I would use jQuery personally). 添加/删除标记时,您可以使用JavaScript更新lat / long输入(我会亲自使用jQuery)。 At this point you could make the lat/long inputs hidden or read-only - unless there's a reason for your users to update the lat/long manually. 此时,您可以将lat / long输入隐藏或只读 - 除非您的用户有理由手动更新lat / long。

FYI - Google might suggest using V3 of the Maps API but when I tried using it there were too many missing pieces. 仅供参考 - 谷歌可能建议使用Maps API的V3,但是当我尝试使用它时,有太多缺失的部分。 I'd stick with V2. 我坚持使用V2。

I don't know the specifics, but I would check out the Google Maps API for a callback function that is called when a user drops a marker. 我不知道具体细节,但我会查看Google Maps API以获取在用户删除标记时调用的回调函数。 I am sure there is one. 我相信有一个。 When that callback function is called, it will be passed with the latitude and longitude. 当调用该回调函数时,它将以纬度和经度传递。 With that you can update your form attributes with the correct values. 有了它,您可以使用正确的值更新表单属性。

Just to help you get in the right direction. 只是为了帮助您找到正确的方向。

If you are trying to use the Google Maps API in one of your Rails projects, then I would highly recommend using the YM4R plugin . 如果您尝试在某个Rails项目中使用Google Maps API,那么我强烈建议您使用YM4R插件 The plugin provides a nice ruby-friendly object encapsulation of the Google Maps API. 该插件提供了一个很好的ruby友好对象封装的Google Maps API。 The README in the Rdoc has great exmample code for how to get started displaying the map and displaying one or more markers. Rdoc中自述文件具有很好的例子,用于说明如何开始显示地图并显示一个或多个标记。

The big advantage of using the plugin is that the plugin abstracts the entire GMaps API behind ruby objects so that you can create and add to the map using only Ruby code. 使用该插件的最大优点是该插件将整个GMaps API抽象为ruby对象,因此您只需使用Ruby代码即可创建并添加到地图中。 You won't have to write any Javascript...unless you want to ;) 你不必写任何Javascript ...除非你想;)

Cartographer plugin supports Rails 3+ , Cartographer插件支持Rails 3+,

It has been re-written to work for huge numbe of markers and clusters 它被重写为大量标记和集群的工作

it supports painless integration of google maps v3 in rails , http://grati.la/cartorbflw 它支持google maps v3在rails中的无痛集成, http://grati.la/cartorbflw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM