简体   繁体   English

MKPolyline的问题

[英]Issues with MKPolyline

I am very new to Xcode and I am facing issues in tracing user path using polyline. 我对Xcode还是很陌生,在使用折线跟踪用户路径时遇到了问题。

I am getting locations correctly. 我正确地找到位置。 I am able to add pins properly. 我能够正确添加图钉。 However, my polyline method is never called. 但是,我的折线方法从未调用过。

Below is my code. 下面是我的代码。

In header file... 在头文件中...

@interface Tracker : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
    MKMapView *mapView;
    //other declarations
}

Inside implementation file, I have following code. 在实现文件中,我有以下代码。 I call, drawPolyline method inside didUpdateLocations method, which is called correctly. 我在didUpdateLocations方法内调用了drawPolyline方法,该方法被正确调用。

- (void) drawPolyline:(NSArray *)locations
{
    NSInteger numberOfLocations = [locations count];
    if (numberOfLocations > 1)
    {
        CLLocationCoordinate2D *locationCoordinate2DArray = malloc(numberOfLocations * sizeof(CLLocationCoordinate2D));
        for (int i = 0; i < numberOfLocations; i++)
        {
            CLLocation* current = [locations objectAtIndex:i];
            locationCoordinate2DArray[i] = current.coordinate;
        }

        self.polyline = [MKPolyline polylineWithCoordinates:locationCoordinate2DArray count:numberOfLocations];
        free(locationCoordinate2DArray);

        [mapView addOverlay:self.polyline];
        [mapView setNeedsDisplay];
    }
}

- (MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView* polyLineView = [[MKPolylineView alloc] initWithPolyline:self.polyline];
    polyLineView.strokeColor = [UIColor blueColor];
    polyLineView.lineWidth = 2;
    return polyLineView;
}

Your help is highly appreciated. 非常感谢您的帮助。 Thanks in advance. 提前致谢。

mapView:viewForOverlay: is a delegate method, so you'll need to set the mapview's delegate somewhere. mapView:viewForOverlay:是一个委托方法,因此您需要在某处设置mapview的委托。 Otherwise, the delegate method will never be called. 否则,将永远不会调用委托方法。

[mapView setDelegate:self];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM