Skip to content

Instantly share code, notes, and snippets.

@andyscott
Created June 2, 2018 18:17
Show Gist options
  • Save andyscott/0570845c78a67ea3f9b3eb5922d93a6f to your computer and use it in GitHub Desktop.
Save andyscott/0570845c78a67ea3f9b3eb5922d93a6f to your computer and use it in GitHub Desktop.
package main
import (
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation -framework CoreLocation
#import <CoreLocation/CoreLocation.h>
@interface WhereAmI : NSObject <CLLocationManagerDelegate>
+(WhereAmI *) instance;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (assign, nonatomic) BOOL searching;
@property (strong, nonatomic) CLLocation *currentLocation;
@end
@implementation WhereAmI
+(WhereAmI *) instance
{
static WhereAmI *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
- (id)init {
self = [super init];
if(self != nil) {
self.searching = true;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusAuthorized:
[self.locationManager startUpdatingLocation];
break;
case kCLAuthorizationStatusDenied:
NSLog(@"Location Services are denied");
self.searching = false;
break;
case kCLAuthorizationStatusNotDetermined:
[self.locationManager startUpdatingLocation];
[self.locationManager stopUpdatingLocation];
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"Location services are restricted");
self.searching = false;
break;
default:
break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Location service failed with error %@", error);
self.searching = false;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray*)locations
{
self.currentLocation = [locations lastObject];
self.searching = false;
}
@end
typedef struct {
// latitude in degrees
double latitude;
// longitude in degrees
double longitude;
// altitude in meters
double altitude;
// radius of uncertainty for latitude/longitude, in meters
double horizontalAccuracy;
// accuracy of the altitude, in meters
double verticalAccuracy;
// epoch timestamp
long timestamp;
} Location;
Location *whereAmI() {
while([WhereAmI instance].searching) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, true);
}
CLLocation *cllocation = [WhereAmI instance].currentLocation;
Location *location = malloc(sizeof(Location));
location->latitude = cllocation.coordinate.latitude;
location->longitude = cllocation.coordinate.longitude;
location->altitude = cllocation.altitude;
location->horizontalAccuracy = cllocation.horizontalAccuracy;
location->verticalAccuracy = cllocation.verticalAccuracy;
location->timestamp = cllocation.timestamp.timeIntervalSince1970 * 1000.0;
return location;
}
*/
"C"
"fmt"
)
func main() {
location := C.whereAmI()
fmt.Printf("location: %v\n", location)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment