【问题标题】:Center of Polygon Google Maps JavaScript API v3多边形中心 Google Maps JavaScript API v3
【发布时间】:2016-07-20 08:42:36
【问题描述】:

我浏览了这个帖子:How to get the center of a polygon in google maps v3?,而 Matthew Scharley 的目前标记为正确的答案并不适合我。我正在从服务器中动态下载 50-100 个多边形的角坐标,这些角坐标是一组 lat/long 对象,我需要一个动态而非手动的解决方案来确定多边形中心。 furiozo 来自同一线程的答案接近满足我的需求,因为它计算了多边形的平均中心,但我在 console.log 中从他的函数中看到的结果:

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

console.log(myPolygon.my_getBounds().getCenter());

不是我可以在我的程序中轻易使用的东西。

我在控制台中得到的是:

_.M {}
   lat: function ()
      arguments: (...)
      caller: (...)
      length: 0
      name: ""
      prototype: Object
      __proto__: function ()
      <function scope>
        Closure
          a: 45.256705626
          b: -75.91270512
        Closure 
        Global: Window
   lng: function() -- contains same sub-levels as lat: function() --
   __proto__: Object

我现在真正需要的是如何获取位于我在控制台中的对象中位于 lat: function() 中第一个闭包级别下的 a & b lat/lng 值。 (我已经通过将这些结果替换为代码手动检查,它们代表了我的第一个多边形的正确中心)。任何有关如何联系他们的帮助将不胜感激。

或者,如果有人知道如何动态获取平均多边形中心坐标值的最新解决方案,以便最终结果采用 (lat, lng) 格式,这可能会节省我的时间。

非常感谢!

【问题讨论】:

  • 您看到的是一个 LatLng 对象。为什么不直接使用myPolygon.my_getBounds().getCenter().lat()myPolygon.my_getBounds().getCenter().lng(),还是我误解了请求?
  • @duncan 哇,谢谢!这太容易了!我花了4个小时研究这个。是的,这完全解决了我的问题。我是新手,所以我不知道如何处理这个 LatLng 对象。再次非常感谢!

标签: javascript google-maps google-maps-api-3


【解决方案1】:

您看到的是一个LatLng 对象。

你可以做的就是简单地调用它上面的lat()lng()函数:

var lat = myPolygon.my_getBounds().getCenter().lat();
var lng = myPolygon.my_getBounds().getCenter().lng();

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2013-12-30
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多