【问题标题】:Current position doesn't show up in MapView (iOS)当前位置未显示在 MapView (iOS) 中
【发布时间】:2015-10-01 09:28:23
【问题描述】:

我已经设置了

self.mapView.showsUserLocation = YES;

但是,我仍然看不到地图中的当前图钉。

我错过了什么?

这里是 viewDidLoad

- (void)viewDidLoad {
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    [self.locationManager requestAlwaysAuthorization];
}
#endif

[self.locationManager startUpdatingLocation];

self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];

MKCoordinateRegion region;
region.center.latitude = 25.03;  
region.center.longitude = 121.5;
region.span.latitudeDelta = 0.2;
region.span.longitudeDelta = 0.2;

[self.mapView setRegion:region animated:YES];
}

还有ma​​pView

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation*)userLocation
{
double X= userLocation.coordinate.latitude;
double Y= userLocation.coordinate.longitude;

NSLog(@"%f,%f",X,Y);

CLLocationCoordinate2D currentPosition = [userLocation coordinate];
MKCoordinateRegion region =MKCoordinateRegionMakeWithDistance(currentPosition, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

谢谢!!

【问题讨论】:

  • 您是否要求用户允许使用该位置?
  • 你的意思是报警窗口询问是否允许访问?
  • 请看这个question的答案,也许你会找到答案。
  • 你是在真正的 iOS 设备上还是在模拟器中?
  • @TrevörAnneDenise,在模拟器中

标签: ios objective-c mapkit showuserlocation


【解决方案1】:

iOS8 上的位置授权存在问题。为了解决这个问题,请确保将以下一个或两个键添加到您的 Info.plist 文件中:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

并在您的代码中替换这部分:

#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    [self.locationManager requestAlwaysAuthorization];
}
#endif 

有了这个:

// Add this to solve a location authorization issue on iOS8
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
if ([self._locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self._locationManager requestWhenInUseAuthorization];
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多