使用MapKit框架在谷歌地图上绘制多边形 [英] To draw polygon on google map with MapKit framework
问题描述
我想在地图视图中显示谷歌地图,我想在其上绘制多边形/圆形。
I wanted to display Google map in a map view on which I want to draw a polygon/circle.
任何建议?
推荐答案
我正在阅读您的问题的方式是您希望以编程方式在地图上绘制多边形。为此,请参阅 MapKit上的Apple文档。
The way I'm reading your question is that you want to programmatically draw the polygon on the map. For this, consult the Apple docs on MapKit.
您无需在MapKit地图上添加透明视图(的MKMapView
)。您创建 overlay 对象,在本例中为 MKPolygon
。 (在下面的示例中,变量 map
将是您放置此代码的视图控制器所拥有的 MKMapView
实例in):
You don't need to add transparent views over the MapKit map (MKMapView
). You create an overlay object, in this case an MKPolygon
. (in the following example, the variable map
will be the MKMapView
instance owned by the view controller that you put this code in):
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);
MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
poly.title = @"Colorado";
[map addOverlay:poly];
然后,如果要自定义叠加层的外观(颜色,笔触等),您在拥有 MKMapView
对象的视图控制器中实现 MKMapViewDelegate
协议,并提供<$ c的实现$ c> mapView:viewForOverlay :
Then, if you want to customize the look (colors, stroke, etc.) of the overlay, you implement the MKMapViewDelegate
protocol in the view controller you have that owns the MKMapView
object and provide an implementation of mapView:viewForOverlay
:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView* aView = [[[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay] autorelease];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
当然,请记住将地图实例的委托实际分配给视图控制器(MKMapViewDelegate),可以在界面构建器中,也可以在代码中(例如 viewDidLoad
)。
Of course, always remember to actually assign the map instance's delegate to your view controller (MKMapViewDelegate), either in the interface builder, or in code (e.g. viewDidLoad
).
这篇关于使用MapKit框架在谷歌地图上绘制多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!