LatLng from Google Maps Polygon getPath()

。_饼干妹妹 提交于 2020-01-31 07:07:51

问题


Based on the Google Maps JavaScript API v3 documentation, google.maps.Polygon class's getPath() function returns an MVCArray. In a straightforward case, a Polygon's path can be a single array of LatLngs that are converted to the MVCArray type upon being passed into the google.maps.Polygon class's setPath() function.

The above case is what I'm dealing with currently. I pass in an array of LatLngs, and return (what I assume is) an MVCObject when I call getPath() on my Polygon object. My question is: How do I convert this MVCObject back into a single array of LatLngs that form the Polygon's shape? Is there some built in Google Maps API v3 way that I'm missing? I feel like there has to be some sort of obvious built in conversion function or something in the API that's eluding me.

Any help would be appreciated.


回答1:


When you call Polygon.getPath()api-doc, the return is an MVCArrayapi-doc of LatLng instances that represent the first path of the Polygon. You can directly get to the members of the MVCAarray in two ways:

  1. Call MVCAarray.getArray, which will return the underlying JavaScript Array that contains LatLng members.
  2. Use MVCArray.getAt( index ), which will return whatever is at that index in the MVCArray (a LatLng in this case). This provides you a way to setup a JavaScript for loop to iterate over the members of the array.

You can also indirectly work with the members of the MVCArray by using the forEach(callback:function(*, number)) function. In this case, you must pass a callback function that accepts two parameters:

  1. The actual member element of the MVCArray.
  2. The array index where that element is located.



回答2:


    var polygonBounds = polygon.getPath();
    var bounds = [];
    for (var i = 0; i < polygonBounds.length; i++) {
          var point = {
            lat: polygonBounds.getAt(i).lat(),
            lng: polygonBounds.getAt(i).lng()
          };
          bounds.push(point);
     }


来源:https://stackoverflow.com/questions/11485806/latlng-from-google-maps-polygon-getpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!