如何使用MKOverlayPathView创建路径? [英] How to create a path using MKOverlayPathView?
问题描述
我一直在查看Apple的iOS类参考文档,但不幸的是,没有一个更明智的选择.我已经下载了示例代码KMLViewer
,但是它们使它变得过于复杂...我真正想知道的是如何生成路径并将其添加到MKMapView
.该文档讨论了使用CGPathRef
的问题,但并未真正解释如何使用.
I've been looking at Apple's iOS Class Reference documentation, and am unfortunately none the wiser. I have downloaded their sample code KMLViewer
but they've overcomplicated it... All I really want to know is how to generate a path and add it to the MKMapView
. The documentation talks of using a CGPathRef
, but doesn't really explain how.
推荐答案
以下是生成路径并将其作为覆盖添加到MKMapView
的方法.我将使用MKPolylineView
,它是MKOverlayPathView
的子类,它使您不必引用任何CGPath
,因为您改为创建了MKPolyline
(包含路径的数据)并使用它创建MKPolylineView
(地图上数据的直观表示).
Here's how to generate a path and add it as an overlay to an MKMapView
. I'm going to use an MKPolylineView
, which is a subclass of MKOverlayPathView
and shields you from having to refer to any CGPath
since you instead create an MKPolyline
(containing the data of the path) and use that to create the MKPolylineView
(the visual representation of the data on the map).
必须使用C点数组(MKMapPoint
)或C坐标数组(CLLocationCoordinate2D
)创建MKPolyline
.可惜的是,MapKit不使用更高级的数据结构,例如NSArray
,但是就这样吧!我将假设您具有CLLocation
对象的NSArray
或NSMutableArray
,以演示如何转换为适合MKPolyline
的C数据数组.此数组称为locations
,如何填充它取决于您的应用-例如通过处理用户的触摸位置来填充,或填充从Web服务等下载的数据.
The MKPolyline
has to be created with a C array of points (MKMapPoint
), or a C array of coordinates (CLLocationCoordinate2D
). It's a shame that MapKit doesn't use more advanced data structures such as NSArray
, but so be it! I'm going to assume that you have an NSArray
or NSMutableArray
of CLLocation
objects to demonstrate how to convert to a C array of data suitable for the MKPolyline
. This array is called locations
and how you fill it would be determined by your app - e.g. filled in by processing touch locations by the user, or filled with data downloaded from a web service etc.
在负责MKMapView
的视图控制器中:
In the view controller that is in charge of the MKMapView
:
int numPoints = [locations count];
if (numPoints > 1)
{
CLLocationCoordinate2D* coords = malloc(numPoints * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < numPoints; i++)
{
CLLocation* current = [locations objectAtIndex:i];
coords[i] = current.coordinate;
}
self.polyline = [MKPolyline polylineWithCoordinates:coords count:numPoints];
free(coords);
[mapView addOverlay:self.polyline];
[mapView setNeedsDisplay];
}
请注意,self.polyline在.h中声明为:
Note that self.polyline is declared in the .h as:
@property (nonatomic, retain) MKPolyline* polyline;
此视图控制器还应实现MKMapViewDelegate
方法:
This view controller should also implement the MKMapViewDelegate
method:
- (MKOverlayView*)mapView:(MKMapView*)theMapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView* lineView = [[[MKPolylineView alloc] initWithPolyline:self.polyline] autorelease];
lineView.fillColor = [UIColor whiteColor];
lineView.strokeColor = [UIColor whiteColor];
lineView.lineWidth = 4;
return lineView;
}
您可以使用fillColor,strokeColor和lineWidth属性来确保它们适合您的应用程序.我刚刚在这里用了一条简单的中等宽度的纯白线.
You can play with the fillColor, strokeColor and lineWidth properties to ensure that they are appropriate for your app. I've just gone with a simple, moderately wide plain white line here.
如果您要从地图中删除路径,例如用一些新的坐标来更新它,那么您可以这样做:
If you want to remove the path from the map, e.g. to update it with some new coordinates, then you would do:
[mapView removeOverlay:self.polyline];
self.polyline = nil;
,然后重复上述过程以创建新的MKPolyline,并将其添加到地图中.
and then repeat the above process to create a new MKPolyline and add it to the map.
尽管乍一看,MapKit看上去有些吓人和复杂,但可以轻松地完成一些操作,如本例所示.至少让非C程序员感到恐惧的唯一点是使用malloc创建缓冲区,使用数组语法将CLLocationCoordinates复制到该缓冲区中,然后释放内存缓冲区.
Although on first glance MapKit can look a bit scary and complex, it can be easy to do some things as illustrated in this example. The only scary bit - for non-C programmers at least - is the use of malloc to create a buffer, copy the CLLocationCoordinates into it using array syntax, and then freeing the memory buffer afterwards.
这篇关于如何使用MKOverlayPathView创建路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!