我是一名新手Objective-C。
在我的应用程序中,我正在尝试将用户的路线绘制在地图上。
以下是目前我只能获取用户当前位置的代码:
#import "StartCycleViewController.h"
#import "CrumbPath.h"
@interface StartCycleViewController ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) IBOutlet MKMapView *map;
@property (nonatomic, strong) UIView *containerView;
@end
@implementation StartCycleViewController
@synthesize cycleLocation = _cycleLocation;
@synthesize currentCycleLocation = _currentCycleLocation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startCycleLocation];
_containerView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.containerView];
[self.containerView addSubview:self.map];
// Do any additional setup after loading the view.
}
- (void)dealloc
{
self.locationManager.delegate = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - startCycleLocation
- (void)startCycleLocation{
if (!_cycleLocation){
_cycleLocation = [[CLLocationManager alloc]init];
_cycleLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_cycleLocation.distanceFilter = 10;
_cycleLocation.delegate = self;
}
[_cycleLocation startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"%@",error);
if ( [error code] != kCLErrorLocationUnknown ){
[self stopLocationManager];
}
}
- (void) stopLocationManager {
[self.cycleLocation stopUpdatingLocation];
}
@end
我在网上查阅了一些资料,得知我应该使用MKPolyline
并给它提供坐标。但我不确定如何存储这些地点并使用MKPolyline
来连续绘制这些点,而且在应用程序运行时也需要持续更新。
MKMapView
的委托,你可以在代码中这样做(或者你可以在IB中通过进入“连接检视器”来做)。关于self.locations
的错误是因为你将其定义和/或实例化为NSArray
而不是NSMutableArray
。请使用后者。 - Robmapview:rendererForOverlay:
(或者在Swift中使用mapView(_:rendererFor:)
),并且显然还要设置地图视图的delegate
。如果没有这个方法,就不会渲染任何(可见的)覆盖层。 - Rob