0

I want to retrieve the current position and send it with a POST request to a server. I use mapView.userLocation.location.coordinate but it doesn't work and shows always 0.0 for lat and 0.0 for lng. This is .h of my controller:

#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

and this .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);

}

in Log I always have 0.0 0.0.

I have this method too:

- (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);
}

and in this case the log shows the current position. Someone have an idea?

Alex
  • 23
  • 3
  • if you are running in the simulator make sure it will provide a location... – Volker Apr 08 '14 at 11:40
  • yes, I do this yet. And Shows User Location is checked – Alex Apr 08 '14 at 11:42
  • 1
    you should call the server request in the didUpdateUserLocation delegate method of mapview – AppleDelegate Apr 08 '14 at 11:42
  • but in this way the request is call anytime the userLocation is update. – Alex Apr 08 '14 at 11:44
  • I want to send the request only when I open the map – Alex Apr 08 '14 at 11:45
  • did u tried in real device, in simulator, it shows at (0,0) only – Chandru Apr 08 '14 at 11:46
  • I tried and for the first log shows (0.0) but for the log inside didUpdateUserLocation shows the right userLocation – Alex Apr 08 '14 at 11:49
  • caching of gps data, check the timestamp and dismiss anything older than 15s for example – Volker Apr 08 '14 at 11:50
  • This has _nothing_ to do with the simulator vs. device. The simulator _simulates_ location. This has been asked and answered so many times: You cannot expect the user location to be ready immediately on startup. You must read it in the delegate method (or check that userLocation.location is not nil before using). In the delegate method, set a BOOL to do it once or check accuracy/timestamp. –  Apr 08 '14 at 11:56
  • Eg. http://stackoverflow.com/questions/4941981/mkmapviews-user-location-is-wrong-on-startup-or-resume, http://stackoverflow.com/questions/3945742/check-user-location-age-in-mapkit-gps-accuracy-for-mapkit-user-location-signif, etc. –  Apr 08 '14 at 11:56

0 Answers0