MKMapView 的折线不起作用

Polyline for MKMapView not working

提问人:Jobalisk 提问时间:11/16/2016 更新时间:11/16/2016 访问量:512

问:

我一直在尝试并尝试让这条多边形线在目标 C 中工作。我已经浏览了我能找到的所有教程,但一无所获,我看不出它有什么问题。

    CLLocationCoordinate2D coordinateArray2[2]; 
coordinateArray2[1].longitude = 176.8773669;
coordinateArray2[0].longitude = 176.88151896;
coordinateArray2[0].latitude = -39.668593;
coordinateArray2[1].latitude = -39.67018069;
_route1Line = [MKPolyline polylineWithCoordinates:coordinateArray2 count:2]; 
[_GPSView setDelegate:self];
[_GPSView addOverlay:_route1Line];
_testlabel.text = [NSString stringWithFormat:@"%f", coordinateArray2[0].longitude];

谁能看出出问题所在?

页眉:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>


extern int routenumber; //holds current route ID

extern BOOL inRoute; //holds if on route or not

extern int followYou; //holds if the app should follow the user or predefined coordinates

extern NSArray *routeHolder; //holds array of routes

@interface ViewController : UIViewController <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *GPSView; //adding the map view to the controller

@property (nonatomic, retain) MKPolyline *route1Line; //line for this route

@property (weak, nonatomic) IBOutlet UILabel *testlabel;

@end
Objective-C MKMapView MKPileLine

评论

0赞 Jobalisk 11/16/2016
对不起,忽略评论

答:

0赞 Alex 11/16/2016 #1

我目前不在 Mac 上,所以我无法运行您的代码,但看起来您已经创建了 MKPolyline,但您缺少 MKPolylineView

_polylineView = [MKPolylineView alloc] initWithPolyline:route1Line]; // Declare it in your header file or in the implementation's interface.
_polylineView.strokeColor = [UIColor redColor];
_polylineView.lineWidth = 5.0

然后,确保符合 MKMapViewDelegate 并实现 mapView:viewForOverlay

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    return _polylineView;
}

评论

0赞 OlDor 11/16/2016
您正在建议一种已弃用的方法,最好看看我的答案
3赞 OlDor 11/16/2016 #2

您应该实现以下方法:MKMapViewDelegate

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKOverlayPathRenderer *theOverlayPathRenderer;
    {
        theOverlayPathRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        theOverlayPathRenderer.lineWidth = ...;
        theOverlayPathRenderer.fillColor = ...;
        theOverlayPathRenderer.strokeColor = ...;
    }
    return theOverlayPathRenderer;
}