我对 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
对象到地图视图。
例如,您可以执行以下操作:
#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
。所以将你的视图控制器设置为delegate
你的MKMapView
,然后您可以执行如下操作:
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)