嗨,我是IOS的初学者,在我的项目中,我试图在用户步行或沿着公路行驶时构建路线
我正在尝试从GPS获取经度和纬度值,但是根不是构造,这意味着多线不在坐标之间构建,当我运行应用程序异常即将到来时请帮助我一些(如果有错误的话)请发布工作代码而不是它)
#import "ViewController.h"
#import "MyAnnotation.h"
#define spanone 0.01f;
@interface ViewController ()
{
NSMutableArray* MainAnnotations;
NSString * latitude;
NSString * longtude;
}
@property (nonatomic, strong) MKPolylineView *lineView;
@property (nonatomic, strong) MKPolyline *polyline;
@end
@implementation ViewController
@synthesize mapobj;
- (void)viewDidLoad {
[super viewDidLoad];
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
if ([locmanager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[locmanager requestAlwaysAuthorization];
}
[locmanager setDistanceFilter:100.0f];
[locmanager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locmanager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
longtude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"latitude value %@",latitude);
NSLog(@"longtude value %@",longtude);
}
[self createAnnotations];
}
- (void)createAnnotations
{
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = [latitude doubleValue];
center.longitude = [longtude doubleValue];
//span
MKCoordinateSpan span;
span.latitudeDelta = spanone
span.longitudeDelta = spanone;
//Myregion
myRegion.center = center;
myRegion.span = span;
[mapobj setRegion:myRegion animated:YES];
//create annotations
CLLocationCoordinate2D myannotations;
myannotations.latitude = [latitude doubleValue];
myannotations.longitude = [longtude doubleValue];
MainAnnotations = [[NSMutableArray alloc]init];
CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(myannotations.latitude, myannotations.longitude);
[MainAnnotations addObject:[NSValue valueWithMKCoordinate:new_coordinate]];
NSLog(@"array list %lu",(unsigned long)MainAnnotations.count);
MyAnnotation * annotation = [[MyAnnotation alloc]init];
annotation.coordinate = myannotations;
annotation.title = @"Hi";
annotation.subtitle = @"how are you";
[mapobj addAnnotations:[NSArray arrayWithObjects:annotation,nil]];
CLLocationCoordinate2D coordinate = [[MainAnnotations objectAtIndex:0] MKCoordinateValue];
NSLog(@"array values %lu",coordinate);
NSLog(@"main values is %d",mainValue);
[self drawPolyline];
}
-(void)drawPolyline
{
[mapobj removeOverlay:self.polyline];
// create an array of coordinates from allPins
CLLocationCoordinate2D coordinates[MainAnnotations.count];
int i = 0;
for (MyAnnotation *currentLoc in MainAnnotations) {
coordinates[i] = currentLoc.coordinate;
i++;
}
// create a polyline with all cooridnates
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:MainAnnotations.count];
[mapobj addOverlay:polyline];
self.polyline = polyline;
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.polyline)
{
//if we have not yet created an overlay view for this overlay, create it now.
if (self.lineView) {
[self.lineView removeFromSuperview];
}
self.lineView = [[MKPolylineView alloc] initWithPolyline:self.polyline];
self.lineView.fillColor = [UIColor redColor];
self.lineView.strokeColor = [UIColor greenColor];
self.lineView.lineWidth = 10;
overlayView = self.lineView;
}
return overlayView;
}
@end