本文介绍了使用这种格式在Google地图中建立多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google地图中显示多边形.我有一个.xlsx文件,其列名为多边形",其中的坐标是一种奇怪的格式.

例如: [[],[[-68.0913,-38.95585],[-68.09148,-38.95666],[-68.07378,-38.9591],[-68.0393,-38.96023],[-68.03909,-38.95884],[-68.03909,-38.95517],[-68.03273,-38.95452],[-68.03288,-38.95122],[-68.03322,-38.94787],[-68.04327,-38.94201],[-68.06786,-38.93913]],[-68.07294,-38.94037],[-68.07719,-38.94237],[-68.07908,-38.94347],[-68.08127,-38.94434],[-68.08457,-38.94739],[-68.1084,-38.9478],[-68.10842,-38.95442],[-68.0914,-38.9549],[-68.09136,-38.95559],[-68.09123,-38.95594]],[[-68.11045,-38.95312]],[[-68.09643,-38.96523]],[-68.0967,-38.95809],[-68.09688,-38.95342],[-68.07472,-38.95842],[-68.04073,-38.95897],[-68.03989,-38.95899],[-68.03897,-38.95901],[-68.0391,-38.96296],[-68.04457,-38.96303],[-68.04461,-38.96304],[-68.04488,-38.97065],[-68.04485,-38.97065],[-68.04489,-38.97132],[-68.05176,-38.97112],[-68.05704,-38.97265],[-68.05725,-38.97858],[-68.06837,-38.97866],[-68.06886,-38.97315],[-68.06901,-38.96554],[-68.07631,-38.96542]]] .

是否可以使用此数据结构构建多边形或格式不正确?

解决方案

您的数据是类似于GeoJSON的坐标格式.

要将其转换为Google Maps Polygon:

 //遍历(多边形的)外部数组for(var i = 0; i< coords.length; i ++){//LatLng坐标的数组var polygonCoords = [];//遍历数组中的每个坐标.//GeoJSON为[经度,纬度]for(var j = 0; j< coords [i] .length; j ++){var pt = new google.maps.LatLng(coords [i] [j] [1],coords [i] [j] [0])geometricCoords.push(pt);}//构造多边形.var多边形=新的google.maps.Polygon({路径:polygonCoords,strokeColor:#FF0000",strokeOpacity:0.8,重量:2,fillColor:'#FF0000',fillOpacity:0.35,地图:地图});}} 

代码段:

 //本示例创建一个表示百慕大三角的简单多边形.函数initMap(){var map = new google.maps.Map(document.getElementById('map'),{变焦:5中央: {纬度:24.886,lng:-70.268},mapTypeId:'地形'});var coords = [[],[[-68.0913,-38.95585],[-68.09148,-38.95666],[-68.07378,-38.9591],[-68.0393,-38.96023],[-68.03909,-38.95884],[-68.03909,-38.95517],[-68.03273,-38.95452],[-68.03288,-38.95122],[-68.03322,-38.94787],[-68.04327,-38.94201],[-68.06786,-38.93913],[-68.07294,-38.94037],[-68.07719,-38.94237],[-68.07908,-38.94347],[-68.08127,-38.94434],[-68.08457,-38.94739],[-68.1084,-38.9478],[-68.10842,-38.95442],[-68.0914,-38.9549],[-68.09136,-38.95559],[-68.09123,-38.95594]],[[-68.11045,-38.95312]],[[-68.09643,-38.96523],[-68.0967,-38.95809],[-68.09688,-38.95342],[-68.07472,-38.95842],[-68.04073,-38.95897],[-68.03989,-38.95899],[-68.03897,-38.95901],[-68.0391,-38.96296]],[-68.04457,-38.96303],[-68.04461,-38.96304],[-68.04488,-38.97065],[-68.04485,-38.97065],[-68.04489,-38.97132],[-68.05176,-38.97112],[-68.05704,-38.97265],[-68.05725,-38.97858],[-68.06837,-38.97866],[-68.06886,-38.97315],[-68.06901,-38.96554],[-68.07631,-38.96542]]];var bounds = new google.maps.LatLngBounds();for(var i = 0; i< coords.length; i ++){var polygonCoords = [];for(var j = 0; j< coords [i] .length; j ++){var pt = new google.maps.LatLng(coords [i] [j] [1],coords [i] [j] [0])bounds.extend(pt);geometricCoords.push(pt);}//构造多边形.var多边形=新的google.maps.Polygon({路径:polygonCoords,strokeColor:#FF0000",strokeOpacity:0.8,重量:2,fillColor:'#FF0000',fillOpacity:0.35,地图:地图});}map.fitBounds(bounds);}  
 /*始终明确设置地图高度以定义div的大小*包含地图的元素.*/#地图 {高度:100%;}/*可选:使示例页面填充窗口.*/html,身体 {高度:100%;边距:0;填充:0;}  
 < div id ="map"></div><!-用您自己的API密钥替换key参数的值.-><脚本异步延迟src ="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>  

I'm trying to display polygons in google map. I have a .xlsx file with a column named "polygon" in which are polygon coordinates in a strange format.

For example: [[], [[-68.0913, -38.95585], [-68.09148, -38.95666], [-68.07378, -38.9591], [-68.0393, -38.96023], [-68.03909, -38.95884], [-68.03909, -38.95517], [-68.03273, -38.95452], [-68.03288, -38.95122], [-68.03322, -38.94787], [-68.04327, -38.94201], [-68.06786, -38.93913], [-68.07294, -38.94037], [-68.07719, -38.94237], [-68.07908, -38.94347], [-68.08127, -38.94434], [-68.08457, -38.94739], [-68.1084, -38.9478], [-68.10842, -38.95442], [-68.0914, -38.9549], [-68.09136, -38.95559], [-68.09123, -38.95594]], [[-68.11045, -38.95312]], [[-68.09643, -38.96523], [-68.0967, -38.95809], [-68.09688, -38.95342], [-68.07472, -38.95842], [-68.04073, -38.95897], [-68.03989, -38.95899], [-68.03897, -38.95901], [-68.0391, -38.96296], [-68.04457, -38.96303], [-68.04461, -38.96304], [-68.04488, -38.97065], [-68.04485, -38.97065], [-68.04489, -38.97132], [-68.05176, -38.97112], [-68.05704, -38.97265], [-68.05725, -38.97858], [-68.06837, -38.97866], [-68.06886, -38.97315], [-68.06901, -38.96554], [-68.07631, -38.96542]]].

Is it possible to build a polygon with this data structure or it's an incorrect format?

解决方案

Your data is a GeoJSON like coordinates format.

To translate that into a Google Maps Polygon:

// go through the outer array (of polygons)
for (var i = 0; i < coords.length; i++) {
    // array for the LatLng coordinates
    var polygonCoords = [];
    // go through each coordinate in the array.
    // GeoJSON is [longitude,latitude]
    for (var j = 0; j < coords[i].length; j++) {
      var pt = new google.maps.LatLng(coords[i][j][1], coords[i][j][0])
      polygonCoords.push(pt);
    }
    // Construct the polygon.
    var polygon = new google.maps.Polygon({
      paths: polygonCoords,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map
    });
  }
}

proof of concept fiddle

code snippet:

// This example creates a simple polygon representing the Bermuda Triangle.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 5,
    center: {
      lat: 24.886,
      lng: -70.268
    },
    mapTypeId: 'terrain'
  });

  var coords = [[], [[-68.0913, -38.95585], [-68.09148, -38.95666], [-68.07378, -38.9591], [-68.0393, -38.96023], [-68.03909, -38.95884], [-68.03909, -38.95517], [-68.03273, -38.95452], [-68.03288, -38.95122], [-68.03322, -38.94787], [-68.04327, -38.94201], [-68.06786, -38.93913], [-68.07294, -38.94037], [-68.07719, -38.94237], [-68.07908, -38.94347], [-68.08127, -38.94434], [-68.08457, -38.94739], [-68.1084, -38.9478], [-68.10842, -38.95442], [-68.0914, -38.9549], [-68.09136, -38.95559], [-68.09123, -38.95594]], [[-68.11045, -38.95312]], [[-68.09643, -38.96523], [-68.0967, -38.95809], [-68.09688, -38.95342], [-68.07472, -38.95842], [-68.04073, -38.95897], [-68.03989, -38.95899], [-68.03897, -38.95901], [-68.0391, -38.96296], [-68.04457, -38.96303], [-68.04461, -38.96304], [-68.04488, -38.97065], [-68.04485, -38.97065], [-68.04489, -38.97132], [-68.05176, -38.97112], [-68.05704, -38.97265], [-68.05725, -38.97858], [-68.06837, -38.97866], [-68.06886, -38.97315], [-68.06901, -38.96554], [-68.07631, -38.96542]]];


  var bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < coords.length; i++) {
    var polygonCoords = [];
    for (var j = 0; j < coords[i].length; j++) {
      var pt = new google.maps.LatLng(coords[i][j][1], coords[i][j][0])
      bounds.extend(pt);
      polygonCoords.push(pt);
    }
    // Construct the polygon.
    var polygon = new google.maps.Polygon({
      paths: polygonCoords,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map
    });
  }
  map.fitBounds(bounds);
}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */

#map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

这篇关于使用这种格式在Google地图中建立多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 01:47