LocationProvider.m 1.06 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "LocationProvider.h"

7 8 9 10 11
#import <CoreLocation/CoreLocation.h>

@implementation LocationProvider {
    CLLocationManager* _locationManager;
}
12 13 14 15 16 17 18 19 20 21 22

@synthesize messageName = _messageName;

- (instancetype) init {
    self = [super init];
    if (self)
        self->_messageName = @"getLocation";
    return self;
}

- (NSString*)didReceiveString:(NSString*)message {
23 24 25 26 27 28 29
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager startMonitoringSignificantLocationChanges];
    }

    CLLocation* location = _locationManager.location;

30
    NSDictionary* response = @{
31 32
        @"latitude": @(location.coordinate.latitude),
        @"longitude": @(location.coordinate.longitude),
33 34 35 36 37 38 39
    };

    NSData* data = [NSJSONSerialization dataWithJSONObject:response options:0 error:nil];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

@end