簡體   English   中英

將SVG多邊形路徑轉換為經緯度

[英]Convert SVG polygon path to lat and long coordinates

tl; dr:如何將SVG多邊形路徑轉換為gps坐標或相同的google.maps.Polygon?

我的高級目標是確定20,000個單獨的GPS坐標是否在固定區域(地理范圍)內。 我已經保存了gps坐標,並且對如何測試它們是否在該區域中有了基本的了解。

我現在正在嘗試將路徑轉換為GPS緯度/經度,我確定這只是一個公式,我需要調和0,0和比例-這個答案很有希望: https: //stackoverflow.com/a/24171749/550595 ,我曾將其用於“轉換”,但結果甚至還很接近。

我有一個SVG多邊形路徑,該路徑是我從另一張地圖(不是google地圖,但距離足夠近)中借來的,並在地圖上渲染了它: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM