Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Last active November 5, 2017 22:45
Show Gist options
  • Save jniemann66/001f94c8bd4b8e567dacc5c2e724f458 to your computer and use it in GitHub Desktop.
Save jniemann66/001f94c8bd4b8e567dacc5c2e724f458 to your computer and use it in GitHub Desktop.
C++ wrapper for iOS location service
  • include ioslocationservice.h in your C++ project

  • (recommendation only) put the ios stuff (IOSLocationServiceSingleton.mm and IOSLocationServiceSingleton.h)

    in it's own ios subfolder within your C++ project

    • use like this:
    #include "ioslocationservice.h"
    IOSLocationService::LocationService iosLocationService;
    Loc loc = iosLocationService.getCurrentLocation();
    
  • don't forget to put an entry in info.plist to get user permission to allow location services:

 <key>NSLocationWhenInUseUsageDescription</key>
        <string>Location services uses your location for ... (reason) </string>
#ifndef IOSLOCATIONSERVICE_H
#define IOSLOCATIONSERVICE_H
// definition for very basic C++ interface for ios location service
namespace IOSLocationService {
struct Loc {
double latitude;
double longitude;
};
class LocationService {
public:
LocationService();
Loc getCurrentLocation();
};
} // namespace IOSLocationService
#endif // IOSLOCATIONSERVICE_H
#ifndef IOSLocationServiceSingleton_H
#define IOSLocationServiceSingleton_H 1
// locationservice.h : defines a CLLocationManager Singleton to share location services
@import Foundation;
@import CoreLocation;
@interface IOSLocationServiceSingleton : NSObject <CLLocationManagerDelegate>
+(IOSLocationServiceSingleton *) sharedInstance;
@property (retain, nonatomic) CLLocationManager *locationManager;
@property (retain, nonatomic) CLLocation *currentLocation;
- (void)startUpdatingLocation;
- (void)stopUpdatingLocation;
@end
#endif // IOSLocationServiceSingleton_H
#include "../ioslocationservice.h"
#include "IOSLocationServiceSingleton.h"
// implementation of a global location services singleton
@implementation IOSLocationServiceSingleton
+(IOSLocationServiceSingleton *) sharedInstance
{
static IOSLocationServiceSingleton *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (id)init {
NSLog(@"[ObjC] Initializing instance of Location Service");
self = [super init];
if(self != nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100; // meters
self.locationManager.delegate = self;
// pick an authorization method:
[self.locationManager requestWhenInUseAuthorization];
// [self.locationManager requestAlwaysAuthorization];
}
return self;
}
- (void)startUpdatingLocation
{
NSLog(@"Starting location updates");
[self.locationManager startUpdatingLocation];
}
- (void)stopUpdatingLocation {
NSLog(@"Stopping location updates");
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
#pragma unused(manager)
NSLog(@"Location service failed with error %@", error);
}
- (void)dealloc {
[self.locationManager stopUpdatingLocation];
NSLog(@"Deallocating IOSLocationServiceSingleton");
[super dealloc];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray*)locations
{
#pragma unused(manager)
CLLocation *location = [locations lastObject];
NSLog(@"Location Update: Latitude %+.6f, Longitude %+.6f\n",
location.coordinate.latitude,
location.coordinate.longitude);
[self setCurrentLocation:location];
}
@end
///////////////////////
// C++ wrapper
namespace IOSLocationService {
LocationService::LocationService() {
[[IOSLocationServiceSingleton sharedInstance] startUpdatingLocation];
}
Loc LocationService::getCurrentLocation() {
CLLocation* clloc = [IOSLocationServiceSingleton sharedInstance].currentLocation;
Loc loc;
loc.latitude = clloc.coordinate.latitude;
loc.longitude = clloc.coordinate.longitude;
return loc;
}
} // namespace IosLocationService
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment