現在位置を取得し、POST リクエストでサーバーに送信したいと考えています。使用mapView.userLocation.location.coordinate
していますが、機能せず、緯度は常に 0.0、経度は 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
そしてこれ .m :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
_shops = [[NSMutableArray alloc] init];
[_loadingIcon startAnimating];
CLLocationCoordinate2D coordinate;
coordinate = _mapView.userLocation.location.coordinate;
NSNumber *lat = [NSNumber numberWithDouble:coordinate.latitude];
NSNumber *lng = [NSNumber numberWithDouble:coordinate.longitude];
//here the post method
NSArray *keys = [NSArray arrayWithObjects:@"lat", @"lng", nil];
NSArray *objects = [NSArray arrayWithObjects:lat,lng, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSData *jsonData ;
NSString *jsonString;
if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
.....
NSLog(@"posizione %@", jsonString);
}
ログには常に 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);
}
この場合、ログには現在の位置が表示されます。誰かがアイデアを持っていますか?