2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-03-10 01:05:10 +00:00
|
|
|
|
|
|
|
#import "RCTMap.h"
|
|
|
|
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
const CLLocationDegrees RCTMapDefaultSpan = 0.005;
|
|
|
|
const NSTimeInterval RCTMapRegionChangeObserveInterval = 0.1;
|
|
|
|
const CGFloat RCTMapZoomBoundBuffer = 0.01;
|
|
|
|
|
|
|
|
@implementation RCTMap
|
2015-03-26 04:29:28 +00:00
|
|
|
{
|
|
|
|
UIView *_legalLabel;
|
|
|
|
CLLocationManager *_locationManager;
|
|
|
|
}
|
2015-03-10 01:05:10 +00:00
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
2015-03-26 04:29:28 +00:00
|
|
|
if ((self = [super init])) {
|
2015-04-15 00:51:28 +00:00
|
|
|
|
2015-05-07 23:20:08 +00:00
|
|
|
_hasStartedRendering = NO;
|
2015-04-15 00:51:28 +00:00
|
|
|
|
2015-03-10 01:05:10 +00:00
|
|
|
// Find Apple link label
|
|
|
|
for (UIView *subview in self.subviews) {
|
|
|
|
if ([NSStringFromClass(subview.class) isEqualToString:@"MKAttributionLabel"]) {
|
2015-04-15 00:51:28 +00:00
|
|
|
// This check is super hacky, but the whole premise of moving around
|
|
|
|
// Apple's internal subviews is super hacky
|
2015-03-10 01:05:10 +00:00
|
|
|
_legalLabel = subview;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2015-03-26 04:29:28 +00:00
|
|
|
[_regionChangeObserveTimer invalidate];
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 12:29:06 +00:00
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
self.frame = frame;
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:05:10 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
|
|
|
if (_legalLabel) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
CGRect frame = _legalLabel.frame;
|
|
|
|
if (_legalLabelInsets.left) {
|
|
|
|
frame.origin.x = _legalLabelInsets.left;
|
|
|
|
} else if (_legalLabelInsets.right) {
|
2015-04-16 12:29:06 +00:00
|
|
|
frame.origin.x = self.frame.size.width - _legalLabelInsets.right - frame.size.width;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
|
|
|
if (_legalLabelInsets.top) {
|
|
|
|
frame.origin.y = _legalLabelInsets.top;
|
|
|
|
} else if (_legalLabelInsets.bottom) {
|
2015-04-16 12:29:06 +00:00
|
|
|
frame.origin.y = self.frame.size.height - _legalLabelInsets.bottom - frame.size.height;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
|
|
|
_legalLabel.frame = frame;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Accessors
|
|
|
|
|
|
|
|
- (void)setShowsUserLocation:(BOOL)showsUserLocation
|
|
|
|
{
|
|
|
|
if (self.showsUserLocation != showsUserLocation) {
|
|
|
|
if (showsUserLocation && !_locationManager) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_locationManager = [CLLocationManager new];
|
2015-03-10 01:05:10 +00:00
|
|
|
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
|
|
|
|
[_locationManager requestWhenInUseAuthorization];
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 00:51:28 +00:00
|
|
|
super.showsUserLocation = showsUserLocation;
|
2015-03-10 01:05:10 +00:00
|
|
|
|
|
|
|
// If it needs to show user location, force map view centered
|
|
|
|
// on user's current location on user location updates
|
2015-04-15 00:51:28 +00:00
|
|
|
_followUserLocation = showsUserLocation;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 12:29:06 +00:00
|
|
|
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
|
2015-03-10 01:05:10 +00:00
|
|
|
{
|
2015-03-26 04:29:28 +00:00
|
|
|
// If location is invalid, abort
|
|
|
|
if (!CLLocationCoordinate2DIsValid(region.center)) {
|
|
|
|
return;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 04:29:28 +00:00
|
|
|
// If new span values are nil, use old values instead
|
|
|
|
if (!region.span.latitudeDelta) {
|
|
|
|
region.span.latitudeDelta = self.region.span.latitudeDelta;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
2015-03-26 04:29:28 +00:00
|
|
|
if (!region.span.longitudeDelta) {
|
|
|
|
region.span.longitudeDelta = self.region.span.longitudeDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Animate to new position
|
2015-04-16 12:29:06 +00:00
|
|
|
[super setRegion:region animated:animated];
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 16:07:19 +00:00
|
|
|
- (void)setAnnotations:(RCTPointAnnotationArray *)annotations
|
2015-04-15 00:51:28 +00:00
|
|
|
{
|
2015-08-17 14:35:34 +00:00
|
|
|
NSMutableArray *newAnnotationIds = [NSMutableArray new];
|
|
|
|
NSMutableArray *annotationsToDelete = [NSMutableArray new];
|
|
|
|
NSMutableArray *annotationsToAdd = [NSMutableArray new];
|
2015-06-25 16:07:19 +00:00
|
|
|
|
|
|
|
for (RCTPointAnnotation *annotation in annotations) {
|
|
|
|
if (![annotation isKindOfClass:[RCTPointAnnotation class]]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
[newAnnotationIds addObject:annotation.identifier];
|
|
|
|
|
|
|
|
// If the current set does not contain the new annotation, mark it as add
|
|
|
|
if (![self.annotationIds containsObject:annotation.identifier]) {
|
|
|
|
[annotationsToAdd addObject:annotation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RCTPointAnnotation *annotation in self.annotations) {
|
|
|
|
if (![annotation isKindOfClass:[RCTPointAnnotation class]]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the new set does not contain an existing annotation, mark it as delete
|
|
|
|
if (![newAnnotationIds containsObject:annotation.identifier]) {
|
|
|
|
[annotationsToDelete addObject:annotation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (annotationsToDelete.count) {
|
|
|
|
[self removeAnnotations:annotationsToDelete];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (annotationsToAdd.count) {
|
|
|
|
[self addAnnotations:annotationsToAdd];
|
|
|
|
}
|
|
|
|
|
2015-08-17 14:35:34 +00:00
|
|
|
NSMutableArray *newIds = [NSMutableArray new];
|
2015-06-25 16:07:19 +00:00
|
|
|
for (RCTPointAnnotation *anno in self.annotations) {
|
|
|
|
if ([anno isKindOfClass:[MKUserLocation class]]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[newIds addObject:anno.identifier];
|
2015-04-15 00:51:28 +00:00
|
|
|
}
|
2015-06-25 16:07:19 +00:00
|
|
|
self.annotationIds = newIds;
|
2015-04-15 00:51:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 01:05:10 +00:00
|
|
|
@end
|