简体   繁体   中英

Get GPS coordinates in Obj-C

I'm trying to get actual GPS coordinates (latitude and longitude) in custom class. I'm working on getting actual weather informations based on user localization and I need to fill API URL with GPS coordinates.

I can't find any informations about that, all tutorials are about real-time GPS data refreshing. Would you want to help me?

First, everything you need is in what you have already investigated: CoreLocation.

For an application like weather, it doesn't change (typically) in a matter of feet/meters, but over larger scales, so instead of getting "real time" changes, you'll request "significant changes".

See this excerpt from https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html :

Getting the User's Current Location

The Core Location framework lets you locate the current position of the device and use that information in your app. The framework reports the device's location to your code and, depending on how you configure the service, also provides periodic updates as it receives new or improved data.

Two services can give you the user's current location:

  • The standard location service is a configurable, general-purpose solution for getting location data and tracking location changes for the specified level of accuracy.
  • The significant-change location service delivers updates only when there has been a significant change in the device's location, such as 500 meters or more. Gathering location data is a power-intensive operation. For most apps, it's usually sufficient to establish an initial position fix and then acquire updates only periodically after that. Regardless of the importance of location data in your app, you should choose the appropriate location service and use it wisely to avoid draining a device's battery. For example:

If your iOS app must keep monitoring location even while it's in the background, use the standard location service and specify the location value of the UIBackgroundModes key to continue running in the background and receiving location updates. (In this situation, you should also make sure the location manager's pausesLocationUpdatesAutomatically property is set to YES to help conserve power.) Examples of apps that might need this type of location updating are fitness or turn-by-turn navigation apps. If GPS-level accuracy isn't critical for your app and you don't need continuous tracking, use the signicant-change location service. This service can save power by not tracking continuously and only waking the device and launching your app when significant changes occur.

Below that section in the docs, you'll see sample code under the heading:

Starting the Significant-Change Location Service

Understanding these pieces combined with your research (and possibly geocoding, turning coordinates into placemarks data such as postal code) should give you everything you need for your weather API.

I would show my actual code. It has worked once and then I always get 0.000000. Why?

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@import CoreLocation;

@interface ViewController : UIViewController <CLLocationManagerDelegate>
    @property (strong, nonatomic) CLLocationManager *locationManager;
    - (IBAction)button:(id *)sender;
@end

And here is my implementation file

#import "ViewController.h"
#import "CoreLocation/CoreLocation.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad 
  {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.delegate = self;
  }

- (void)didReceiveMemoryWarning 
  {
    [super didReceiveMemoryWarning];
  }

- (IBAction)button:(id *)sender 
  {
    [self.locationManager startUpdatingLocation];
    NSLog(@"%f", self.locationManager.location.coordinate.latitude);
  }

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM