[英]How to save a marker with latitude and longitude into mysql database using Ruby on Rails
I use google maps api v3 with Ruby on rails 3.1. 我在Ruby on Rails 3.1上使用google maps api v3。 I have the following model:
我有以下模型:
class Business < ActiveRecord::Base
attr_accessible :business_name, :address, :city_id,
:latitude, :longitude, :zipcode
Part of my show view of the business is here: 我的部分业务展示视图在这里:
%style{:type => "text/css"}
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
%script{:type => "text/javascript",
:src => "http://maps.googleapis.com/maps/api/js?v=3.6&sensor=false"}
%script{:type => "text/javascript"}
function initialize() {
var latlng = new google.maps.LatLng(#{@business.latitude}, #{@business.longitude});
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var contentString = "#{@business.business_name}"
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: latlng,
draggable:true,
animation: google.maps.Animation.DROP,
title:"#{@business.business_name}"
});
// To add the marker to the map, call setMap();
marker.setMap(map);
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
google.maps.event.addListener(marker, 'dragend', function() {
var latitude = marker.getPosition().lat();
var longitude = marker.getPosition().lng();
// use Ajax to save latitude, longitude in the database
}
}
google.maps.event.addDomListener(window, 'load', initialize);
%div#map_canvas{:style => "width:500px; height:350px"}
I can drag and drop a marker, but I still can't save the new position in the database (mysql 2.8.1). 我可以拖放一个标记,但是仍然不能在数据库中保存新位置(mysql 2.8.1)。 I found some javascript (see lines around var latitude = marker.getPosition().lat();), but since my javascript knowledge is very poor I have no idea how to save the new marker position in the database.
我找到了一些javascript(请参见var latitude = marker.getPosition()。lat();周围的行),但是由于我的javascript知识很差,所以我不知道如何在数据库中保存新的标记位置。 Ideally I would like to have a "save" link in my Info Window which would confirm the storage of the new latitude and longitude into the database.
理想情况下,我希望在“信息窗口”中有一个“保存”链接,以确认将新的经度和纬度存储到数据库中。
Does anybody know how to do this? 有人知道怎么做这个吗? Thanks a lot in advance for your help!
在此先感谢您的帮助!
You clould transfer the data to an ajax form: 您应该将数据传输为ajax格式:
<%= form_for @marker, :remote => true, :html => { :multipart => true } do |f| %>
<%= f.hidden_field :lat, :id => "lat" %>
<%= f.hidden_field :lng, :id => "lng" %>
<%= f.submit "save marker" %>
<% end %>
in your js add: 在您的js中添加:
var lat = $("#lat");
var lng = $("#lng");
var set_latlng_to_input = function(marker){
lat.val(marker.getPosition().lat());
lng.val(marker.getPosition().lng());
}
this function you now have to call in your google.maps.event.addListener: 现在,您必须在google.maps.event.addListener中调用此函数:
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
set_latlng_to_input(marker);
});
google.maps.event.addListener(marker, 'dragend', function() {
set_latlng_to_input(marker);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.