简体   繁体   中英

MKMapView location won't update

After posting on Apple's iOS Devs forums with no answer, I'm trying to see if anyone's got any experience with such issue. I'm using xcode for iOS 7.

MapView won't update. I'm quite sure the problem's within the simulator, though I'm not sure how to check it. I'd love to take this project on a real device but my iOS 7 device's still in store.

I created a MKMapView nested under a View (nested under a ViewController) -> Added CoreLocation and MapKit into Frameworks -> Connected the MKMapView to the right property mapView.

My files look like this:

MapScanViewController.h:

#import <UIKit/UIKit.h>    
#import <MapKit/MapKit.h>

@interface MapScanViewController : UIViewController<MKMapViewDelegate>

// I was trying to create a label and print the 
// userLocation description into the label - but none worked
@property (nonatomic,retain) IBOutlet UILabel *myLabel;

@property (nonatomic,strong) IBOutlet MKMapView *mapView;

@end

And MapScanViewController.m :

#import "MapScanViewController.h"

@implementation MapScanViewController

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

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);

    [_mapView setRegion:[_mapView regionThatFits:region] animated:YES];

    // Label idea did not work eiter
    [_myLabel setText:[userLocation description]];    
}

@end

When I try to change location to Apple's Infinite loop, the map stays still like nothing's changing at all. Also tried to use bicycle ride and running but nothing happened. I tried to reset the iOS Simulator few times which did not help at all and of course tried to manually set the location from within xcode it-self.

Tried to add/force this (which did not help either):

if (_mapView.showsUserLocation)
{
_mapView.showsUserLocation = NO;
_mapView.showsUserLocation = YES;
}

None worked for me. MapView just wont respond.


I verified the map view

// Meters-Miles convertion
#define METERS_PER_MILE 1609.344

// Just wanted to check if MapView's dead or not - worked- it took me to see (39.28..., -76.58...)
- (void)viewWillAppear:(BOOL)animated {
    // 1
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 39.281516;
    zoomLocation.longitude= -76.580806;

    // 2
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

    // 3
    [_mapView setRegion:viewRegion animated:YES];
}

you have to call _mapView.showsUserLocation = YES; before you get anything from the map.

That call will for prompt the user for access to location services.

As Daij-Djan said Try like this

- (void)viewDidLoad    
{
 _mapView.showsUserLocation = YES;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM