How to get LatLngBounds of feature polygon geometry in google maps v3?

前端 未结 3 1280
耶瑟儿~
耶瑟儿~ 2020-12-18 19:24

I have a lot of polygonal features loaded with loadGeoJson and I\'d like to get the latLngBounds of each. Do I need to write a function that iterates through every lat long

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 20:17

    In Google Maps JavaScript API v2, Polygon had a getBounds() method, but that doesn’t exist for v3 Polygon. Here’s the solution:

    if (!google.maps.Polygon.prototype.getBounds) {
        google.maps.Polygon.prototype.getBounds = function () {
            var bounds = new google.maps.LatLngBounds();
            this.getPath().forEach(function (element, index) { bounds.extend(element); });
            return bounds;
        }
    }
    

提交回复
热议问题