質問

I have three views that are subviews of the ViewController's view. Three labels toggling their respective visibility. In the "near me" feature I would like to display only items that are within 10-mile span user's current location.

In ViewController.h

@property (weak, nonatomic) IBOutlet MKMapView *nearMeMapView;
@property (strong, nonatomic) CLLocationManager *locationManager;

In ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.locationManager.delegate = self;
    self.nearMeMapView.delegate = self;
} 

- (IBAction)nearMeTap:(id)sender 
{
    self.nearMeMapView.hidden = NO;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    [self.locationManager startUpdatingLocation];
    NSLog(@"near me tapped")
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = newLocation.coordinate.latitude;
    zoomLocation.longitude= newLocation.coordinate.latitude;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 10*METERS_PER_MILE, 10*METERS_PER_MILE);
    [self.nearMeMapView setRegion:viewRegion animated:YES];
}

Anyway, the problem is that location of the device is detected correctly but MapView zooms and animates to entirely wrong location.

役に立ちましたか?

解決

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = newLocation.coordinate.latitude;
    //This was set wrong in your function
    zoomLocation.longitude= newLocation.coordinate.longitude;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 10*METERS_PER_MILE, 10*METERS_PER_MILE);
    [self.nearMeMapView setRegion:viewRegion animated:YES];
}

The commented line was your problem you were setting your zoom.longitude to a latitude position!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top