我有一组 GPolygon 对象,它们附加到我的域模型中的对象,并添加到我的页面上的地图中。域对象显示在同一页面上的列表中,当用户单击其中一个时,我想显示关联的多边形。
我希望显示整个多边形,理想情况下我希望地图以多边形中心为中心。
我希望能有一个地图 API 调用,
myMap.makeSureYouCanSeeAllThisPolygon(aPolygon);
但我一直没能找到。
如果我必须手动执行此操作,那么我显然可以轻松地确定居中,但是如何确定缩放呢?
I have a set of GPolygon objects which are attached to objects in my domain model and are added to a map on my page. The domain objects are being shown in a list on the same page and when the user clicks on one of them I want to show the associated polygon.
I want the whole polygon to be showing and ideally I want the map to be centred on the polygon centre.
I was hoping that there would be a maps API call along the lines of...
myMap.makeSureYouCanSeeAllThisPolygon(aPolygon);
but I haven't been able to find one.
If I have to do this manually, then I can obviously figure out the centering easily, but how do I figure out the zoom?
发布评论
评论(6)
Google Maps v3 API 本身不支持 google.maps.Polygon 类的
getBounds()
方法。这看起来很奇怪,因为 google.maps.Map 类有一个fitBounds()
方法,这正是您正在寻找的。如果您将 Google Maps API 与任何类型一起使用那么这应该在你的技巧包中::google.maps.Polygon 类的
getBounds()
方法有了这个方法,就可以非常简单地在地图上居中并拟合多边形。
注意:如果您不熟悉
getAt()
和getLength()
也没关系,它们是 google.maps.MVCArray 类特有的方法。当您调用多边形的getPaths()
方法时,它会将您的 LatLng 数组作为 LatLng 的可变 MVCArray 返回。您可以在此处准备有关 MVCArrays 的信息 - Google 地图 v3 API MVCArray 类。继续前进。这是框架,您必须在下一段代码之前的某个地方实现上面的原型方法。
舞台设置完毕后,您只需将此(或任何)多边形居中并缩放即可::
Google Maps v3 API doesn't natively support a
getBounds()
method for the google.maps.Polygon class. This seems strange given the fact that the google.maps.Map class has afitBounds()
method, which is exactly what you're looking for.. If you use the Google Maps API with any kind of frequency then this should be in your bag of tricks ::getBounds()
method for the google.maps.Polygon classHaving this method on hand makes it very simple to center and fit a polygon on the map.
Note : If you're not familiar with
getAt()
andgetLength()
that's fine, they're methods unique to the google.maps.MVCArray class. When you call thegetPaths()
method of a polygon it returns your array of LatLng's as a mutable MVCArray of LatLng's.. you can ready about MVCArrays here - Google Maps v3 API MVCArray class.Moving on. Here's the framework, you'll have to implement the prototyped method above somewhere before this next bit of code.
With the stage set, all you'd have to do to center and zoom on this (or any) polygon is ::
我编写了一个函数来添加到 Kevin James 解决方案中。它需要一个多边形数组并获取地图上可能拥有的所有多边形的边界。你只需要把它们推到一个数组上,然后你就可以调用我的函数来自动缩放地图。
I wrote a function to add to Kevin James solution. it takes a polygon array and gets the bounds for all the polygons you may have on your map. you just need to push them onto an array and then you can call my function to auto zoom the map.
您可以使用
aPolygon.getBounds().getCenter();
获取多边形的中心点,然后使用myMap.setCenter( 返回的
方法。GLatLng
)要获取缩放级别,您可以使用
myMap.getBoundsZoomLevel(aPolygon.getBounds());
然后将其与myMap.setZoom()
方法一起使用。You can get the centre point of a polygon by using
aPolygon.getBounds().getCenter();
and then using theGLatLng
returned with themyMap.setCenter()
method.To get the zoom level, you can use
myMap.getBoundsZoomLevel(aPolygon.getBounds());
then use this withmyMap.setZoom()
method.认为他说:
工作是一种享受。
think he ment:
works a treat.
该解决方案适用于我已成功添加到地图中的多边形。为 google.maps.Polygon 类添加 getBounds() 方法,然后使用 map.fitBounds(myPolygon.getBounds());
This solution worked for the polygons I already successfully added to the map. Adding the getBounds() method for the google.maps.Polygon class, and then using map.fitBounds(myPolygon.getBounds());
用 ES6 编写的 getBounds 函数的较短版本
使地图居中
A shorter version from the getBounds function written in ES6
To center the map