Javascript Google Maps API多边形区域

Javascript Google Maps API多边形区域,javascript,google-maps-api-3,geometry,Javascript,Google Maps Api 3,Geometry,我试图找到多边形的面积。这在加载多边形时发生,并将在函数中使用。这是我的密码: map.data.forEach(function(feature) { if (feature.getGeometry().getType() == "Polygon") { var bounds=[]; feature.getGeometry().forEachLatLng(function(path) { var temp = new google

我试图找到多边形的面积。这在加载多边形时发生,并将在函数中使用。这是我的密码:

  map.data.forEach(function(feature) {
    if (feature.getGeometry().getType() == "Polygon") {
        var bounds=[];
        feature.getGeometry().forEachLatLng(function(path) {
          var temp = new google.maps.LatLng(path.lat,path.lng);
          bounds.push(temp);

        });
        console.log(bounds);
        console.log(google.maps.geometry.spherical.computeArea(bounds));

    }
});
如果加载的对象是多边形,它将生成一个latlng数组,然后应使用computeArea函数。我一直从这件事中得到回报。我查看了帮助文档,计算区域采用了latLngs区域,这就是边界

边界显示正确,因此我不确定为什么没有计算面积

该函数返回一个值。它没有
.lat
.lng
属性(您不需要将其转换为
google.maps.LatLng
,它已经是一个),这些都是函数

这对我很有用:

map.data.forEach(function(feature) {
    if (feature.getGeometry().getType() == "Polygon") {
        var bounds=[];
        feature.getGeometry().forEachLatLng(function(path) {
          bounds.push(path);
        });
        console.log(bounds);
        console.log(google.maps.geometry.spherical.computeArea(bounds));
    }
});

代码片段:

函数初始化(){
var map=new google.maps.map(
document.getElementById(“地图画布”){
缩放:4,
中心:{
拉丁语:-28,
液化天然气:137
},
mapTypeId:google.maps.mapTypeId.ROADMAP
});
map.data.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json', {},
功能(特性){
map.data.forEach(函数(特性){
if(feature.getGeometry().getType()==“Polygon”){
var界限=[];
var polyBnds=new google.maps.LatLngBounds();
feature.getGeometry().forEachLatLng(函数(路径){
边界。推(路径);
扩展(路径);
});
console.log(边界);
var area=google.maps.geometry.sphereal.computeArea(边界);
控制台日志(区域);
var iW=new google.maps.InfoWindow({
内容:面积。固定(2)+“平方米”,
职位:polyBnds.getCenter()
});
开放(地图);
}
});
});
}
google.maps.event.addDomListener(窗口“加载”,初始化)
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}

该函数返回一个值。它没有
.lat
.lng
属性(您不需要将其转换为
google.maps.LatLng
,它已经是一个),这些都是函数

这对我很有用:

map.data.forEach(function(feature) {
    if (feature.getGeometry().getType() == "Polygon") {
        var bounds=[];
        feature.getGeometry().forEachLatLng(function(path) {
          bounds.push(path);
        });
        console.log(bounds);
        console.log(google.maps.geometry.spherical.computeArea(bounds));
    }
});

代码片段:

函数初始化(){
var map=new google.maps.map(
document.getElementById(“地图画布”){
缩放:4,
中心:{
拉丁语:-28,
液化天然气:137
},
mapTypeId:google.maps.mapTypeId.ROADMAP
});
map.data.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json', {},
功能(特性){
map.data.forEach(函数(特性){
if(feature.getGeometry().getType()==“Polygon”){
var界限=[];
var polyBnds=new google.maps.LatLngBounds();
feature.getGeometry().forEachLatLng(函数(路径){
边界。推(路径);
扩展(路径);
});
console.log(边界);
var area=google.maps.geometry.sphereal.computeArea(边界);
控制台日志(区域);
var iW=new google.maps.InfoWindow({
内容:面积。固定(2)+“平方米”,
职位:polyBnds.getCenter()
});
开放(地图);
}
});
});
}
google.maps.event.addDomListener(窗口“加载”,初始化)
html,
身体,
#地图画布{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}