从mapView ios获取用户位置

从mapView ios获取用户位置,ios,mkmapview,userlocation,Ios,Mkmapview,Userlocation,我想检索当前位置并将其与POST请求一起发送到服务器。 我使用mapView.userLocation.location.coordinate,但它不起作用,lat总是显示0.0,lng总是显示0.0。 这是我的控制器的.h: #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> @interface CustomViewController : UIViewController <MKMapViewDelegate

我想检索当前位置并将其与POST请求一起发送到服务器。 我使用
mapView.userLocation.location.coordinate
,但它不起作用,lat总是显示0.0,lng总是显示0.0。 这是我的控制器的.h:

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

@interface CustomViewController : UIViewController 
<MKMapViewDelegate>

@property NSMutableArray * shops;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *loadingIcon;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

在日志中,我始终使用0.0 0.0

我也有这个方法:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation: (MKUserLocation *)userLocation
{
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    userLocation.title = @"Posizione attuale";
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2000, 2000);
    [_mapView setRegion:region animated:YES];

    NSLog(@"lat %f", (double)userLocation.location.coordinate.latitude);
}
在这种情况下,日志显示当前位置。
有人有主意吗?

如果您在模拟器中运行,请确保它将提供一个位置……是的,我还没有这样做。并显示选中了用户位置您应该在MapView的didUpdateUserLocation委托方法中调用服务器请求,但通过这种方式,只要用户位置更新,请求就会被调用。我只想在打开地图时发送请求
    NSLog(@"posizione %@", jsonString);

}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation: (MKUserLocation *)userLocation
{
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    userLocation.title = @"Posizione attuale";
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2000, 2000);
    [_mapView setRegion:region animated:YES];

    NSLog(@"lat %f", (double)userLocation.location.coordinate.latitude);
}