MKMapView 的折线不起作用
Polyline for MKMapView not working
我一直在努力让这条折线在 Objective 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];
谁能看出哪里出了问题?
header:
#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
我现在不在 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;
}
您应该实现 MKMapViewDelegate
的以下方法:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKOverlayPathRenderer *theOverlayPathRenderer;
{
theOverlayPathRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
theOverlayPathRenderer.lineWidth = ...;
theOverlayPathRenderer.fillColor = ...;
theOverlayPathRenderer.strokeColor = ...;
}
return theOverlayPathRenderer;
}
我一直在努力让这条折线在 Objective 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];
谁能看出哪里出了问题?
header:
#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
我现在不在 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;
}
您应该实现 MKMapViewDelegate
的以下方法:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKOverlayPathRenderer *theOverlayPathRenderer;
{
theOverlayPathRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
theOverlayPathRenderer.lineWidth = ...;
theOverlayPathRenderer.fillColor = ...;
theOverlayPathRenderer.strokeColor = ...;
}
return theOverlayPathRenderer;
}