Can';t通过Objective-C使用MapKit获取当前用户位置

Can';t通过Objective-C使用MapKit获取当前用户位置,objective-c,mapkit,mkuserlocation,Objective C,Mapkit,Mkuserlocation,我在这里设置区域坐标: // 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.lo

我在这里设置区域坐标:

// 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;
CGFLoat CurrentLatitude = self.locationManager.location.coordinate.latitude;
CGFloat CurrentLongitude = self.locationManager.location.coordinate.longitude;
}

我知道这只会放大定义的区域,但是使用类似的方法,有没有办法获得当前用户位置而不是像上面那样的一组预定义坐标

我想让地图找到当前用户的位置,然后在地图上缩放到它,如果有意义的话

#import <CoreLocation/CoreLocation.h>
然后,一旦检索到位置,就会触发此委托方法

 -(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
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置{
[self.locationManager停止更新位置];
CGFLoat usersLatitude=self.locationManager.location.coordinate.latitude;
CGFloat usersLongidute=self.locationManager.location.coordinate.longitude;
//现在你有了你用户的共同目标
}

我希望这有帮助。

在didUpdateLocation方法中添加这两行:

-(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;
CGFLoat CurrentLatitude = self.locationManager.location.coordinate.latitude;
CGFloat CurrentLongitude = self.locationManager.location.coordinate.longitude;

请参阅《位置和地图编程指南》中的。谢谢。在IBAction方法中设置locatioManager是什么意思?嘿,Jim=-我让它工作了,但它一直在重新居中返回用户位置?为什么?嗨@MichaelSebastian你从哪里打电话给startUserLocationSearch?听起来你又在叫这个了。如果您只需要一次位置,那么尝试将其设置为从按钮操作调用,因为一旦它点击委托方法,“StopUpdateLocation”指令将停止再次搜索(直到再次请求start search方法),这几乎只是重复已经说过的话。