转换SVG多边形路径到纬度和长坐标

Convert SVG polygon path to lat and long coordinates

本文关键字:坐标 纬度 SVG 多边形 路径 转换      更新时间:2023-09-26

tl;dr:我如何将SVG多边形路径转换为gps坐标,或相同的google.maps.Polygon?

我的高级目标是确定是否有20,000个单独的GPS坐标在固定区域(地理围栏)内。我已经保存了gps坐标,并且我对如何测试它们是否在该区域有一个基本的想法。

我现在正在尝试将路径转换为GPS的纬度/长度,我确信这只是一个公式,我需要调和0,0和比例-这个答案是有希望的:https://stackoverflow.com/a/24171749/550595,我用于"转换",但结果甚至不接近。

我有一个SVG多边形路径,我从一个不同的地图(不是谷歌地图,但它足够接近),我渲染它在地图上:https://jsfiddle.net/2n2jmj8L/1/

var width = 624, // width of SVG
  height = 746; // height of SVG
function toLongitude(x) {
  return x * 180 / width;
}
function toLatitude(y) {
  return -y * 180 / width + 90;
}
function initMap() {
  // The SVG path;
  var path = document.getElementById("reservation-path").getAttribute("d");
  // Get the points. Even indices are x coordinates and odd indices are y
  // coordinates. Note that these are strings.
  var points = path.match(/('d+)/g);
  map = new google.maps.Map(document.getElementById('map'), {
    streetViewControl: false,
    //scrollwheel: false,
    disableDefaultUI: true,
    draggable: false,
    zoom: 11,
    center: {
      lat: 41.3671863,
      lng: -123.8799729
    },
    mapTypeId: 'roadmap'
  });
  // Map polygon coordinates
  var polyCoordinates = [];
  for (var i = 0; i < points.length; i += 2) {
    var longitude = toLongitude(parseInt(points[i]) + 6),//I added the 6 and the following 5 to offset the parent transform
      latitude = toLatitude(parseInt(points[i + 1]) + 5);
    polyCoordinates.push(new google.maps.LatLng(latitude, longitude));
  }
  console.log(polyCoordinates);
  var polygon = new google.maps.Polygon({
    paths: polyCoordinates,
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35
  });
  polygon.setMap(map);
  // Add a listener for the click event.
  polygon.addListener('click', showArrays);
  infoWindow = new google.maps.InfoWindow;
}

顺便说一下…Google Maps/Places概述了我想使用作为我的地理围栏的区域:https://www.google.com/maps/place/Yurok+Reservation,+Trinity-Klamath,+CA/@41.373847,-123.9471508,11z/data=!4m5!3m4!1s0x54d1a8156f591fd5:0xbcbca4e17d7d8801!8m2!3d41.3724029!4d-123.9009415,但我无法做任何事情,除了使用它作为参考。

我已经在这上面浪费了太多时间,所以即使我需要放弃我所拥有的,我也会这么做。我所要做的就是能够循环遍历一组坐标,如果它们在JSFiddle的路径中,则返回。

这个页面似乎记录了如何将屏幕像素坐标转换为纵横坐标。

https://developers.google.com/maps/documentation/javascript/examples/map-coordinates