简体   繁体   English

如何使用Ruby on Rails将经度和纬度标记保存到mysql数据库中

[英]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.

相关问题 谷歌地图添加标记到地图,然后在Ruby on Rails中存储纬度和经度 - Google Maps Add a Marker to Map, then Store Latitude and Longitude in Ruby on Rails 如何获取地址的经纬度并保存到数据库 - How to get latitude and longitude of an address and save to database 在Ruby On Rails中获取经度和纬度 - Get Latitude and Longitude in Ruby On Rails 如何通过使用htlm和Java脚本从Firebase数据库检索的纬度和经度在地图上显示标记 - How to show a marker on map by using latitude and longitude retrieved from firebase database in htlm and java script 如何在一个 map 中显示 3 个纬度和经度(标记)? - how to display 3 latitude & longitude (Marker) in one map? 如何在我的数据库中保存纬度、经度。 使用传单地图 - How to save latitude, longitude in my db. Using leaflet map 通过MVCArray循环以在数据库中保存纬度和经度 - Looping though an MVCArray to save Latitude and Longitude in the database 使用 SQL 数据库和 Leaflet Map 在 Z9E0DA8438E1E38B81CDD3 - Display Marker Using Latitude And Longitude From SQL Database With Leaflet Map In ASP.NET CORE MVC 从数据库中获取多个经度并在Google地图上显示其标记 - fetch multiple latitude and longitude from database and display their marker on google map 使用jQuery插件的Google Maps API:如何在点击时获取标记的纬度和经度? - Google Maps API using jQuery Plugin: How to get a marker's latitude and longitude on click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM