【问题标题】:Can't get current user location using MapKit via Objective-C无法通过 Objective-C 使用 MapKit 获取当前用户位置
【发布时间】:2016-04-22 12:14:39
【问题描述】:

我在这里设置区域坐标:

// Center Map Here on initial load
#define CENTER_LATITUDE 22.11111  #fake for example
#define CENTER_LONGITUDE -23.99292 #fake for example

我在这里设置区域缩放:

MKCoordinateRegion region;
region.center.latitude = CENTER_LATITUDE;
region.center.longitude = CENTER_LONGITUDE;
region.span.latitudeDelta = SPAN_VALUE;
region.span.longitudeDelta = SPAN_VALUE;
[self.mapView setRegion:region animated:NO];

这是我的 mapView didUpdateUserLocation 方法:

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation 
  *)userLocation {

  CLLocationCoordinate2D loc = [userLocation coordinate];
  MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
  [self.mapView setRegion:region animated:YES];

  self.mapView.centerCoordinate = userLocation.location.coordinate;

}

我知道这只会放大定义的区域,但是使用与此类似的东西,有没有办法获取当前用户位置,而不是像上面那样设置预定义坐标?

我希望地图找到当前用户位置,然后在地图上放大到该位置,如果这有意义的话。

【问题讨论】:

标签: objective-c mapkit mkuserlocation


【解决方案1】:

在 didUpdateLocation 方法中添加这两行:

CGFLoat CurrentLatitude = self.locationManager.location.coordinate.latitude;
CGFloat CurrentLongitude = self.locationManager.location.coordinate.longitude;

【讨论】:

【解决方案2】:
#import <CoreLocation/CoreLocation.h>

记得在类文件中包含 LocationManager 委托

@interface YourViewControllerClass ()<CLLocationManagerDelegate>

@property (strong, nonatomic)  CLLocationManager *locationManager;

@end

当您准备好搜索位置时设置您的 locationManager - 从 IBAction 方法甚至 viewDidAppear 等中。

-(void)startUserLocationSearch{

     self.locationManager = [[CLLocationManager alloc]init];
     self.locationManager.delegate = self;
     self.locationManager.distanceFilter = kCLDistanceFilterNone;
     self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    //Remember your pList needs to be configured to include the location persmission - adding the display message  (NSLocationWhenInUseUsageDescription)

     if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
         [self.locationManager requestWhenInUseAuthorization];
     }
     [self.locationManager startUpdatingLocation];
}

然后,一旦检索到位置,这个委托方法就会触发。

 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

      [self.locationManager stopUpdatingLocation];
      CGFLoat usersLatitude = self.locationManager.location.coordinate.latitude;
      CGFloat usersLongidute = self.locationManager.location.coordinate.longitude;

      //Now you have your user's co-oridinates
}

我希望这会有所帮助。

【讨论】:

  • 谢谢。在 IBAction 方法中设置 locatioManager 是什么意思?
  • 嘿,吉姆 =- 我让它工作了,但它一直重新回到用户位置?这是为什么呢?
  • 嗨@MichaelSebastian 你从哪里调用startUserLocationSearch?听起来你又在打电话了。如果您只需要一次位置,则尝试将其设置为从按钮操作调用,因为一旦它击中委托方法,“stopUpdatingLocation”指令就会再次停止搜索(直到再次请求开始搜索方法)
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 2014-09-03
  • 2017-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
相关资源
最近更新 更多