地图注释不会显示在正确的位置

时间:2014-03-26 19:54:46

标签: ios core-location mkannotation

我希望在我的坐标发生变化时进行注释,但是所有注释都会在海洋中间转到相同的错误坐标。有人可以测试和帮助吗?

WhereamiViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "BNRMapPoint.h"

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate >
{
    CLLocationManager *locationManager;

    IBOutlet MKMapView* worldView;
    IBOutlet UIActivityIndicatorView* activityIndicator;
    IBOutlet UITextField* locationTitleField;
}

-(IBAction)Tipos:(id)sender;
-(void)findLocation;
-(void)foundLocation:(CLLocation*)loc;
@end

WhereamiViewController.m

#import "WhereamiViewController.h"

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:(NSCoder*)aDecoder];
    if (self) {
        locationManager = [[CLLocationManager alloc]init];
        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [locationManager setDistanceFilter:50.00];

        if ([CLLocationManager headingAvailable]){
            [locationManager startUpdatingHeading];
            [locationManager setHeadingFilter:5];
        }
        else{
            NSLog(@"Heading is not aviable");
        }
    }
    return self;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)newLocation
{
    NSLog(@"%@",newLocation);

    CLLocation *new = [newLocation lastObject];

    [self foundLocation:new];
}

-(void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@",error);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    NSLog(@"\n Heading Luis: %@",newHeading);
}

-(void)viewDidLoad
{
    [worldView setShowsUserLocation:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //Zoom
    CLLocationCoordinate2D loc = [userLocation coordinate];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self findLocation];
    [textField resignFirstResponder];
    return YES;
}

-(IBAction)Tipos:(id)sender
{
    switch (((UISegmentedControl *)sender).selectedSegmentIndex)
    {
        case 0:
        {
            worldView.mapType = MKMapTypeStandard;
            break;
        }
        case 1:
        {
            worldView.mapType = MKMapTypeSatellite;
            break;
        }
        default:
        {
            worldView.mapType = MKMapTypeHybrid;
            break;
        }
    }
}

-(void)findLocation
{
    [locationManager startUpdatingLocation];
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
}

-(void)foundLocation:(CLLocation *)loc
{
    CLLocationCoordinate2D theCoordinate3 = [loc coordinate];

    NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc]init];
    [aDateFormatter setDateStyle:NSDateFormatterMediumStyle];

    BNRMapPoint *mp = [[BNRMapPoint alloc]initWithCoordinate:theCoordinate3 title:  [locationTitleField text] subtitle:[aDateFormatter stringFromDate:[NSDate date]]];

    [worldView addAnnotation:mp];

    int degrees = mp.coordinate.latitude;
    double decimal = fabs(loc.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
                     degrees, minutes, seconds];

    degrees = mp.coordinate.longitude;
    decimal = fabs(loc.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
                       degrees, minutes, seconds];

    NSLog(@"Coordinate test : %@ lat and %@ long",lat,longt);

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 100, 100);
    [worldView setRegion:region animated:YES];

    locationTitleField.text = @"";
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
}

@end

BNRMapPoint.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface BNRMapPoint : NSObject <MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString*)t subtitle:(NSString*)s;

@end

BNRMapPoint.m

#import "BNRMapPoint.h"

@implementation BNRMapPoint

@synthesize coordinate,title;

-(id)init
{
    return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -8.00) title:@"Hometown" subtitle:@""];
    NSLog(@"Hometown");
}

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString*)t subtitle:(NSString*)s
{
    self = [super init];

    if (self)
    {
        c=coordinate;
        [self setTitle:t];
        [self setSubtitle:s];
    }
    return self;
}

@end

1 个答案:

答案 0 :(得分:0)

BNRMapPoint课程中,在initWithCoordinate:title:subtitle:方法中,此行:

c=coordinate;

应该是:

coordinate = c;

因为您想将实例变量coordinate)设置为传入参数值(c)而不是相反。

由于实例变量永远不会被设置,因此它保持在大西洋的默认0,0。



此外,在initWithCoder:方法中或在WhereamiViewController.m中的某个时刻,您必须调用startUpdatingLocation而不是(或除此之外)调用startUpdatingHeading。否则,永远不会调用didUpdateLocations:委托方法。