人有关于如何使用MKOverlayPathView创建路径的任何

人有关于如何使用MKOverlayPathView创建路径的任何

本文介绍了有没有人有关于如何使用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.

任何人都可以帮助或指点我请问正确的方向?谢谢!

Could anyone help or point me in the right direction please? Thanks!

推荐答案

以下是如何生成路径并将其作为叠加层添加到 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).

MKPolyline 必须使用C数组点( MKMapPoint )或C数组坐标( CLLocationCoordinate2D )。令人遗憾的是,MapKit不使用更高级的数据结构,例如 NSArray ,但也是如此!我将假设你有一个 NSArray NSMutableArray CLLocation 对象,演示如何转换为适合 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.

在负责<$ c $的视图控制器中c> 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创建路径的任何示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:57