Find centerpoint of polygon in JavaScript

后端 未结 5 1186
余生分开走
余生分开走 2020-12-15 08:39

I have a \"place\" object from Google Maps which has a set of coordinates that represent a bounding box for a given location, say London. Each set of coordinates has a latit

5条回答
  •  抹茶落季
    2020-12-15 09:32

    To get the bounds of a Polygon (with your data) in the Google Maps API v3 (not tested):

    var coords = [
      [ -1.2, 5.1 ],
      [ -1.3, 5.2 ],
      [ -1.8, 5.9 ],
      [ -1.9, 5.8 ]
    ];
    
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i

    var coords = [
      [-1.2, 5.1],
      [-1.3, 5.2],
      [-1.8, 5.9],
      [-1.9, 5.8]
    ];
    
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < coords.length; i++) {
      bounds.extend(new google.maps.LatLng(coords[i][0], coords[i][1]));
    }
    var center = bounds.getCenter();
    
    var latitude = center.lat();
    console.log("latitude=" + latitude);
    var longitude = center.lng();
    console.log("longitude=" + longitude);
    var coordinates = center.toUrlValue();
    console.log("coordinates=" + coordinates);

提交回复
热议问题