我是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
在应用程序运行时连续绘制点。
您应该只创建一个 NSMutableArray
,以保持您在viewDidLoad
中实例化的位置。因此,请使用didUpdateToLocation
(或者,如果支持iOS 6及更高版本,则应使用didUpdateToLocations
)只需在数组中添加一个位置,然后从该数组中构建一个MKPolyline
,请在地图中添加该MKPolyline
,然后删除旧的MKPolyline
。或者,您可以将所有行段添加为单个MKPolyline
对象,但是这个想法是相同的,创建一个模型以保持您的位置(例如NSMutableArray
),然后将适当的MKPolyline
对象添加到地图视图中。p>例如,您可以做类似:
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
if (location.horizontalAccuracy < 0)
return;
[self.locations addObject:location];
NSUInteger count = [self.locations count];
if (count > 1) {
CLLocationCoordinate2D coordinates[count];
for (NSInteger i = 0; i < count; i++) {
coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
}
MKPolyline *oldPolyline = self.polyline;
self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:count];
[self.mapView addOverlay:self.polyline];
if (oldPolyline)
[self.mapView removeOverlay:oldPolyline];
}
}
并记住指定映射如何绘制MKPolyline
。因此,将视图控制器设置为MKMapView
的delegate
,然后您可以执行以下操作:
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
renderer.lineWidth = 3;
return renderer;
}
return nil;
}
// for iOS versions prior to 7; see `rendererForOverlay` for iOS7 and later
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];
overlayView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
overlayView.lineWidth = 3;
return overlayView;
}
return nil;
}