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"
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
#import "RCTMapAnnotation.h"
|
|
|
|
#import "RCTMapOverlay.h"
|
2015-03-10 01:05:10 +00:00
|
|
|
#import "RCTUtils.h"
|
2016-06-07 15:36:07 +00:00
|
|
|
#import "UIView+React.h"
|
2015-03-10 01:05:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-07 15:36:07 +00:00
|
|
|
- (void)didUpdateReactSubviews
|
2015-12-17 14:45:53 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
// Do nothing, as annotation views are managed by `setAnnotations:` method
|
2015-12-17 14:45:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 01:05:10 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
|
|
|
if (_legalLabel) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
CGRect frame = self->_legalLabel.frame;
|
|
|
|
if (self->_legalLabelInsets.left) {
|
|
|
|
frame.origin.x = self->_legalLabelInsets.left;
|
|
|
|
} else if (self->_legalLabelInsets.right) {
|
|
|
|
frame.origin.x = self.frame.size.width - self->_legalLabelInsets.right - frame.size.width;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
2016-07-07 19:36:56 +00:00
|
|
|
if (self->_legalLabelInsets.top) {
|
|
|
|
frame.origin.y = self->_legalLabelInsets.top;
|
|
|
|
} else if (self->_legalLabelInsets.bottom) {
|
|
|
|
frame.origin.y = self.frame.size.height - self->_legalLabelInsets.bottom - frame.size.height;
|
2015-03-10 01:05:10 +00:00
|
|
|
}
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_legalLabel.frame = frame;
|
2015-03-10 01:05:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
// TODO: this doesn't preserve order. Should it? If so we should change the
|
|
|
|
// algorithm. If not, it would be more efficient to use an NSSet
|
2015-12-17 14:45:53 +00:00
|
|
|
- (void)setAnnotations:(NSArray<RCTMapAnnotation *> *)annotations
|
2015-04-15 00:51:28 +00:00
|
|
|
{
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
NSMutableArray<NSString *> *newAnnotationIDs = [NSMutableArray new];
|
|
|
|
NSMutableArray<RCTMapAnnotation *> *annotationsToDelete = [NSMutableArray new];
|
|
|
|
NSMutableArray<RCTMapAnnotation *> *annotationsToAdd = [NSMutableArray new];
|
2015-06-25 16:07:19 +00:00
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
for (RCTMapAnnotation *annotation in annotations) {
|
|
|
|
if (![annotation isKindOfClass:[RCTMapAnnotation class]]) {
|
2015-06-25 16:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
[newAnnotationIDs addObject:annotation.identifier];
|
2015-06-25 16:07:19 +00:00
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
// If the current set does not contain the new annotation, mark it to add
|
|
|
|
if (![_annotationIDs containsObject:annotation.identifier]) {
|
2015-06-25 16:07:19 +00:00
|
|
|
[annotationsToAdd addObject:annotation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
for (RCTMapAnnotation *annotation in self.annotations) {
|
|
|
|
if (![annotation isKindOfClass:[RCTMapAnnotation class]]) {
|
2015-06-25 16:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
// If the new set does not contain an existing annotation, mark it to delete
|
|
|
|
if (![newAnnotationIDs containsObject:annotation.identifier]) {
|
2015-06-25 16:07:19 +00:00
|
|
|
[annotationsToDelete addObject:annotation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (annotationsToDelete.count) {
|
2015-11-03 22:45:46 +00:00
|
|
|
[self removeAnnotations:(NSArray<id<MKAnnotation>> *)annotationsToDelete];
|
2015-06-25 16:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (annotationsToAdd.count) {
|
2015-11-03 22:45:46 +00:00
|
|
|
[self addAnnotations:(NSArray<id<MKAnnotation>> *)annotationsToAdd];
|
2015-06-25 16:07:19 +00:00
|
|
|
}
|
|
|
|
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
self.annotationIDs = newAnnotationIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this doesn't preserve order. Should it? If so we should change the
|
|
|
|
// algorithm. If not, it would be more efficient to use an NSSet
|
2015-12-17 14:45:53 +00:00
|
|
|
- (void)setOverlays:(NSArray<RCTMapOverlay *> *)overlays
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
{
|
|
|
|
NSMutableArray *newOverlayIDs = [NSMutableArray new];
|
|
|
|
NSMutableArray *overlaysToDelete = [NSMutableArray new];
|
|
|
|
NSMutableArray *overlaysToAdd = [NSMutableArray new];
|
|
|
|
|
|
|
|
for (RCTMapOverlay *overlay in overlays) {
|
|
|
|
if (![overlay isKindOfClass:[RCTMapOverlay class]]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
[newOverlayIDs addObject:overlay.identifier];
|
|
|
|
|
|
|
|
// If the current set does not contain the new annotation, mark it to add
|
|
|
|
if (![_annotationIDs containsObject:overlay.identifier]) {
|
|
|
|
[overlaysToAdd addObject:overlay];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RCTMapOverlay *overlay in self.overlays) {
|
|
|
|
if (![overlay isKindOfClass:[RCTMapOverlay class]]) {
|
2015-06-25 16:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
|
|
|
|
// If the new set does not contain an existing annotation, mark it to delete
|
|
|
|
if (![newOverlayIDs containsObject:overlay.identifier]) {
|
|
|
|
[overlaysToDelete addObject:overlay];
|
|
|
|
}
|
2015-04-15 00:51:28 +00:00
|
|
|
}
|
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView.
Briefly, if you have a MapView declared as:
<MapView
annotations={this.state.annotations}
overlays={this.state.overlays}
style={styles.map}
region={this.state.region}
ref="mapView"
/>
then setting
this.state.overlays = [{
coordinates: [
{ latitude: 35.5, longitude: -5.5 },
{ latitude: 35.6, longitude: -5.6 },
...
],
strokeColor: 'rgba(255, 0, 0, 0.5)',
lineWidth: 3,
}];
will draw a red line between the points in locations with a width of 3 and equally blended with the background.
Closes https://github.com/facebook/react-native/pull/4153
Reviewed By: svcscm
Differential Revision: D2697347
Pulled By: nicklockwood
fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
2015-11-26 15:09:59 +00:00
|
|
|
|
|
|
|
if (overlaysToDelete.count) {
|
|
|
|
[self removeOverlays:(NSArray<id<MKOverlay>> *)overlaysToDelete];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlaysToAdd.count) {
|
|
|
|
[self addOverlays:(NSArray<id<MKOverlay>> *)overlaysToAdd
|
|
|
|
level:MKOverlayLevelAboveRoads];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.overlayIDs = newOverlayIDs;
|
2015-04-15 00:51:28 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 19:35:53 +00:00
|
|
|
- (BOOL)showsCompass {
|
|
|
|
if ([MKMapView instancesRespondToSelector:@selector(showsCompass)]) {
|
|
|
|
return super. showsCompass;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setShowsCompass:(BOOL)showsCompass {
|
|
|
|
if ([MKMapView instancesRespondToSelector:@selector(setShowsCompass:)]) {
|
|
|
|
super.showsCompass = showsCompass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 01:05:10 +00:00
|
|
|
@end
|