首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能在feature.get中使用MultiLineString几何()

我不能在feature.get中使用MultiLineString几何()
EN

Stack Overflow用户
提问于 2016-07-04 13:12:17
回答 1查看 4.4K关注 0票数 1

我正在使用google v3在地图上绘制GeoJSON。随着层次的点,它是正常运作的。但是我有一个MultiLineString Camda,并且把地图的中心放在几何图形上,给出一个error.This,也是用一个多边形来表示的,但是对于点来说,效果很好。还有其他方法来集中到MultiLineString和多边形吗?

代码语言:javascript
复制
  google.maps.event.addListener(cicloviasLayer, 'addfeature', function (e) {
     console.log(e.feature.getGeometry().getType()); // MultiLineString Ok!
     map.setCenter(e.feature.getGeometry().get());

  });  

错误:

代码语言:javascript
复制
e.feature.getGeometry(...).get is not a function
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-04 14:18:35

Data.MultiLineString类没有get方法,它有一个getAtgetArray方法。

getAt(n:数字) 返回值: Data.LineString 返回包含的n个Data.LineString。

返回的LineString有一个返回google.maps.LatLng对象的getAt方法

getAt(n:数字) 返回值: LatLng 返回包含的n个LatLng。

代码语言:javascript
复制
google.maps.event.addListener(cicloviasLayer, 'addfeature', function (e) {
  console.log(e.feature.getGeometry().getType()); // MultiLineString Ok!
  // will center the map on the first vertex of the first LineString
  map.setCenter(e.feature.getGeometry().getAt(0).getAt(0));
});  

概念小提琴的证明

代码片段:

代码语言:javascript
复制
let map;

function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
    zoom: 15,
    center: {
      lat: -28,
      lng: 137
    },
  });
  map.data.setStyle({
    strokeColor: "blue",
    strokeWeight: 4,
  });
  google.maps.event.addListener(map.data, 'addfeature', function(e) {
    console.log(e.feature.getGeometry().getType()); // MultiLineString Ok!
    // will center the map on the first vertex of the first LineString
    map.setCenter(e.feature.getGeometry().getAt(0).getAt(0));
    var marker = new google.maps.Marker({
      position: e.feature.getGeometry().getAt(0).getAt(0),
      map: map,
      icon: {
        url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
        size: new google.maps.Size(7, 7),
        anchor: new google.maps.Point(3.5, 3.5)
      }
    })
  });
  map.data.addGeoJson({
    "type": "Feature",
    geometry: {
      "type": "MultiLineString",
      "coordinates": [
        [
          [-105.021443,
            39.578057
          ],
          [-105.021507,
            39.577809
          ],
          [-105.021572,
            39.577495
          ],
          [-105.021572,
            39.577164
          ],
          [-105.021572,
            39.577032
          ],
          [-105.021529,
            39.576784
          ]
        ],
        [
          [-105.019898,
            39.574997
          ],
          [-105.019598,
            39.574898
          ],
          [-105.019061,
            39.574782
          ]
        ],
        [
          [-105.017173,
            39.574402
          ],
          [-105.01698,
            39.574385
          ],
          [-105.016636,
            39.574385
          ],
          [-105.016508,
            39.574402
          ],
          [-105.01595,
            39.57427
          ]
        ],
        [
          [-105.014276,
            39.573972
          ],
          [-105.014126,
            39.574038
          ],
          [-105.013825,
            39.57417
          ],
          [-105.01331,
            39.574452
          ]
        ]
      ]
    }
  });
}
代码语言:javascript
复制
/* 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;
}
代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
  <title>Data Layer: Simple</title>
  <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=&v=weekly" defer></script>
  <!-- jsFiddle will insert css and js -->
</head>

<body>
  <div id="map"></div>
</body>

</html>

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38185639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档