flying_tonite
flying_tonite

Reputation: 81

iPhone Getting the wrong coordinates from the GPS

IOS10 on iPhone 6 Plus. A small app app that grabs the gps coordinates and sends them to a remote web service. On the iphone map the user location is showing correctly, but the retrieved coordinates:

location.coordinate.latitude,
location.coordinate.longitude

are 0.5 miles away from where the map says I am! This is consistent wherever I move to.

I am following Apple's best practices and using the delegate method as follows: and it's these coordinates that are incorrect.

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
      didUpdateLocations:(NSArray *)locations {
   CLLocation* location = [locations lastObject];
   NSDate* eventDate = location.timestamp;
   NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
   if (abs(howRecent) < 15.0) {
       // Log the data
      NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
   }
}

I am setting the location accuracy as follows:

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

I sample the gps coordinates for 30 seconds to make sure i am getting the best accuracy possible.

I have tried this on 2 different iPhones, both showing the same issue. Thanks in advance.

Upvotes: 0

Views: 1190

Answers (2)

flying_tonite
flying_tonite

Reputation: 81

I finally found out what is causing the problem.. I am currently in China... and that is the reason for the mysterious offset.. the maps in China are 'offset'. If you are interested there's an post about it here that has saved me from tearing my hair out. http://www.sinosplice.com/life/archives/2013/07/16/a-more-complete-ios-solution-to-the-china-gps-offset-problem.

China uses a mapping projection called GCJ-02 which is different from the West's mapping standard (WGS-84). So if you're ever developing a mapping system, you might want to take this into account for travellers in China!

Thanks anyway for the useful pieces of coding advice.

Upvotes: 1

Prientus
Prientus

Reputation: 733

You can add a check in your method to update locations again until you are down to your desired accuracy:

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
      didUpdateLocations:(NSArray *)locations {
   // You can check for either locationManager.desiredAccuracy or a value you'd like in meters
   CLLocation* location = [locations lastObject];
   if (location.horizontalAccuracy > locationManager.desiredAccuracy) {
       //Do nothing yet
       return; 
   }

   NSDate* eventDate = location.timestamp;
   NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
   if (abs(howRecent) < 15.0) {
       // Log the data
      NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
   }
}

Upvotes: 0

Related Questions