如何在iOS版Google Maps SDK中绘制填充的多边形 [英] how to draw filled polygon in Google Maps SDK for iOS

查看:148
本文介绍了如何在iOS版Google Maps SDK中绘制填充的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在带有Google地图(版本1.1.1,最后一个)的iPhone上绘制一个填充的多边形.

I would like draw a filled polygon on iPhone with Google map (Version 1.1.1, the last one).

任何人都知道如何在ios上做到这一点:

Anyone knows how to do like that on ios :

(我在Android上的代码)

(My code on Android)

mMap.addPolygon(new PolygonOptions()
            .addAll(latLngList)
            .fillColor(Color.BLUE)
            .strokeColor(Color.RED)
            .strokeWidth(3));

此致

PS:如果您有很多解决方案,请记住我要绘制许多多边形.

PS : If you have many solutions, keep in mind that I have many Polygon to draw.

推荐答案

SDK当前不支持填充的多边形,但是有一个功能要求将其添加到此处:

The SDK currently doesn't support filled polygons, however there is a feature request to add them here:

https://code.google.com /p/gmaps-api-issues/issues/detail?id = 5070

同时,一种选择是将多边形绘制到图像中,然后将其添加为地面叠加层.这将是非常有限的,但可以作为临时解决方法.

In the meantime, one option could be to draw your polygons into an image, and then add them as a ground overlay. This would be very limiting, but might work as a temporary workaround.

另一种选择是在地图视图上方添加另一个视图并将多边形绘制到其中,然后在地图视图移动时更新它们.无法将另一个视图与地图视图完美地同步,因此您的多边形在平移/缩放时会滞后一点,但这对您来说也可以作为一种临时解决方法.

Another option is to add another view over the top of the map view and draw the polygons into it, and then update them whenever the map view moves. It isn't possible to perfectly synchronize another view with the map view, so your polygons will lag behind a bit as you pan/zoom around, but this might also be okay for you as a temporary workaround.

更新

这些只是尝试采用地面覆盖方法的一些随机想法,我不确定它们是否可行,但是它们可能会让您入门:

These are just some random ideas to try for the ground overlay approach, I'm not sure if they would work, but they might get you started:

我建议将矩形的纬度/经度角转换为MKMapPoint(使用MKMapPointForCoordinate).这些等效于缩放级别20的Google坐标系.

I would suggest converting the lat/lon corners of the rectangle into MKMapPoint (using MKMapPointForCoordinate). These are equivalent to Google's coordinate system at zoom level 20.

然后可以使用MKMapPoint坐标中矩形的宽度/高度的纵横比来确定地面叠加层UIImage的纵横比.有了宽高比后,您只需尝试实际的尺寸(即猜测一个宽度,然后从宽高比中计算出高度)即可找到合适的尺寸.它越大,矩形的细节越细,但是它将使用更多的内存,并且性能可能会变慢.另外,您可能会在某个尺寸上遇到硬性限制-我猜UIImage被Google Maps SDK转换为纹理,并且在iPhone 3GS +上,纹理的最大尺寸为2048x2048.

You can then use the aspect ratio of the width/height of the rectangle in MKMapPoint coordinates to determine the aspect ratio of your ground overlay UIImage. Once you have the aspect ratio, you'll just need to experiment with actual sizes (ie guess a width, calculate the height from the aspect ratio) to find one which looks okay. The bigger it is, the finer the detail of your rectangle will be, but the more memory it will use, and probably the slower the performance will be. Also you might hit a hard limit at some size - I'm guessing the UIImage gets converted by the Google Maps SDK into a texture, and textures have a max size of 2048x2048 on iPhone 3GS+.

然后,使用类似于>如何通过google设置区域的方法要为iOS映射sdk吗?以计算缩放级别和中心纬度/经度.您可以使用UIImage宽度/高度,而不是地图视图的宽度/高度,而是使用矩形的边界而不是所需视图的边界.您也不需要从宽度和高度上都计算出比例(因为比例应该相同),因此只需使用其中之一即可.不用创建具有缩放级别和中心纬度/经度的相机,而是将它们设置在GMSGroundOverlayOptions上.还要将地面叠加层的anchor设置为图像的中心(即0.5、0.5).

Then, use something similar to How to setRegion with google maps sdk for iOS? to calculate a zoom level and centre lat/lon. Instead of the map view width/height you would use your UIImage width/height, and you'd use the bounds of your rectangle instead of the bounds of the desired view. You also wouldn't need to calculate the scale from both the width and height (as the scale should be the same) - so just use one of them. Instead of creating a camera with the zoom level and centre lat/lon, set them on the GMSGroundOverlayOptions. Also set the ground overlay's anchor to the centre of the image (ie 0.5, 0.5).

上面介绍了如何为每个矩形添加一个GroundOverlay.如果您有很多重叠的矩形或附近的矩形,则可以将它们合并为一个UIImage,但这会有些复杂.

The above describes how to add one GroundOverlay per rectangle. If you have lots of overlapping or nearby rectangles you could probably combine them into a single UIImage, but that would be a bit more complicated.

这篇关于如何在iOS版Google Maps SDK中绘制填充的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆