summaryrefslogtreecommitdiff
path: root/platform/ios/src/MGLUserLocation.m
blob: 245cbf43716c056b42f16003031b37ac4c28d300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#import "MGLUserLocation_Private.h"

#import "MGLMapView.h"
#import "NSBundle+MGLAdditions.h"

NS_ASSUME_NONNULL_BEGIN

@interface MGLUserLocation ()

@property (nonatomic, weak) MGLMapView *mapView;

@end

NS_ASSUME_NONNULL_END

@implementation MGLUserLocation

- (instancetype)initWithMapView:(MGLMapView *)mapView
{
    if (self = [super init])
    {
        _mapView = mapView;
    }

    return self;
}

+ (BOOL)supportsSecureCoding {
    return YES;
}

- (instancetype)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        _location = [decoder decodeObjectOfClass:[CLLocation class] forKey:@"location"];
        _title = [decoder decodeObjectOfClass:[NSString class] forKey:@"title"];
        _subtitle = [decoder decodeObjectOfClass:[NSString class] forKey:@"subtitle"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:_location forKey:@"location"];
    [coder encodeObject:_title forKey:@"title"];
    [coder encodeObject:_subtitle forKey:@"subtitle"];
}

- (BOOL)isEqual:(id)other {
    if (self == other) return YES;
    if (![other isKindOfClass:[MGLUserLocation class]]) return NO;

    MGLUserLocation *otherUserLocation = other;
    return ((!self.location && !otherUserLocation.location) || [self.location distanceFromLocation:otherUserLocation.location] == 0)
    && ((!self.title && !otherUserLocation.title) || [self.title isEqualToString:otherUserLocation.title])
    && ((!self.subtitle && !otherUserLocation.subtitle) || [self.subtitle isEqualToString:otherUserLocation.subtitle]);
}

- (NSUInteger)hash {
    NSUInteger hash = [super hash];
    hash += [_location hash];
    hash += [_heading hash];
    hash += [_title hash];
    hash += [_subtitle hash];
    return hash;
}

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
{
    return ! [key isEqualToString:@"location"] && ! [key isEqualToString:@"heading"];
}

+ (NSSet<NSString *> *)keyPathsForValuesAffectingCoordinate
{
    return [NSSet setWithObject:@"location"];
}

- (void)setLocation:(CLLocation *)newLocation
{
    if ( ! newLocation || ! CLLocationCoordinate2DIsValid(newLocation.coordinate)) return;
    if ( _location && CLLocationCoordinate2DIsValid(_location.coordinate) && [newLocation distanceFromLocation:_location] == 0) return;
    if (newLocation.coordinate.latitude == 0 && newLocation.coordinate.longitude == 0) return;

    [self willChangeValueForKey:@"location"];
    _location = newLocation;
    [self didChangeValueForKey:@"location"];
}

- (BOOL)isUpdating
{
    return self.mapView.userTrackingMode != MGLUserTrackingModeNone;
}

- (void)setHeading:(CLHeading *)newHeading
{
    if (newHeading.trueHeading != _heading.trueHeading)
    {
        [self willChangeValueForKey:@"heading"];
        _heading = newHeading;
        [self didChangeValueForKey:@"heading"];
    }
}

- (CLLocationCoordinate2D)coordinate
{
    return _location ? _location.coordinate : kCLLocationCoordinate2DInvalid;
}

- (NSString *)title
{
    return _title ?: NSLocalizedStringWithDefaultValue(@"USER_DOT_TITLE", nil, nil, @"You Are Here", @"Default user location annotation title");
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: %p; location = %f, %f; updating = %@; altitude = %.0fm; heading = %.0f°; title = %@; subtitle = %@>",
            NSStringFromClass([self class]), (void *)self,
            self.location.coordinate.latitude, self.location.coordinate.longitude,
            self.updating ? @"yes" : @"no",
            self.location.altitude,
            self.heading.trueHeading,
            self.title ? [NSString stringWithFormat:@"\"%@\"", self.title] : self.title,
            self.subtitle ? [NSString stringWithFormat:@"\"%@\"", self.subtitle] : self.subtitle];
}

@end