简体   繁体   English

将Google地图多边形保存为Rails记录

[英]Saving google map polygon as rails record

As rails 3.2.18 postGIS enabled application needs to capture polygon data from google maps API. 作为rails 3.2.18,启用postGIS的应用程序需要从Google Maps API捕获面数据。

The js is as follows: js如下:

  var poly, map;
  var markers = [];
  var path = new google.maps.MVCArray;

  function initialize() {
    var roma = new google.maps.LatLng(41.877, 12.480);

    map = new google.maps.Map(document.getElementById("map"), {
      zoom: 7,
      center: roma,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


    poly = new google.maps.Polygon({
      strokeWeight: 3,
      fillColor: '#5555FF'
    });
    poly.setMap(map);
    poly.setPaths(new google.maps.MVCArray([path]));

    google.maps.event.addListener(map, 'click', addPoint);
  }

  function addPoint(event) {
    path.insertAt(path.length, event.latLng);

    var marker = new google.maps.Marker({
      position: event.latLng,
      map: map,
      draggable: true
    });
    markers.push(marker);
    marker.setTitle("#" + path.length);

    google.maps.event.addListener(marker, 'click', function() {
      marker.setMap(null);
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      markers.splice(i, 1);
      path.removeAt(i);
      }
    );

    google.maps.event.addListener(marker, 'dragend', function() {
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      path.setAt(i, marker.getPosition());
      }
    );
  }

While the map generates the polygon, I would like to, in a first instance view the LatLng points (to track what is being generated), but ultimately save the points. 当地图生成多边形时,我想在第一个实例中查看LatLng点(以跟踪正在生成的内容),但最终要保存这些点。

A first issue is LatLng (Google) vs Lon/Lat (PostGIS). 第一个问题是LatLng(Google)与Lon / Lat(PostGIS)。 While there is always the possibility of flipping the data , I'd just assume avoid it. 尽管总有可能翻转数据 ,但我只是假设避免使用它。 THis would imply some parsing of the data. 这将暗示对数据进行某些解析。

So how can the polygon points get captured to the database, individually or as a LonLat-formatted polygon? 那么如何将多边形点单独或作为LonLat格式的多边形捕获到数据库中? [Bear in mind I'm really not proficient in js...] [记住,我真的不精通js ...]

I'd probably want to make an AJAX request to a ruby script that would then save the details to the DB. 我可能想向ruby脚本发出AJAX请求,然后将详细信息保存到数据库。 You can access the individual latitude and longitude values from your event like so: 您可以像这样访问事件中的各个纬度和经度值:

event.latLng.lat()
event.latLng.lng()

Or indeed use the lat() and lng() functions on anything that returns a LatLng object. 或者实际上在返回LatLng对象的任何对象上都使用lat()lng()函数。 See the docs: https://developers.google.com/maps/documentation/javascript/reference#LatLng 查看文档: https : //developers.google.com/maps/documentation/javascript/reference#LatLng

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

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