Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Libraries/Components/MapView/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ var MapView = React.createClass({
longitudeDelta: React.PropTypes.number.isRequired,
}),

/**
* Coordinates for the pin annotation on the map using title
*/
annotations: React.PropTypes.arrayOf(
React.PropTypes.shape({
latitude: React.PropTypes.number.isRequired,
longitude: React.PropTypes.number.isRequired,
title: React.PropTypes.string
})
),

/**
* Maximum size of area that can be displayed.
*/
Expand Down Expand Up @@ -142,6 +153,7 @@ var MapView = React.createClass({
pitchEnabled={this.props.pitchEnabled}
scrollEnabled={this.props.scrollEnabled}
region={this.props.region}
annotations={this.props.annotations}
maxDelta={this.props.maxDelta}
minDelta={this.props.minDelta}
legalLabelInsets={this.props.legalLabelInsets}
Expand All @@ -165,6 +177,7 @@ var RCTMap = createReactIOSNativeComponentClass({
pitchEnabled: true,
scrollEnabled: true,
region: {diff: deepDiffer},
annotations: {diff: deepDiffer},
maxDelta: true,
minDelta: true,
legalLabelInsets: {diff: insetsDiffer},
Expand Down
Binary file added MapView-PinAnnotations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 54 additions & 1 deletion React/Views/RCTMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ @interface RCTMapManager() <MKMapViewDelegate>

@end

@implementation RCTMapManager
@implementation RCTMapManager{
NSMutableDictionary *pinAnnotations;
}

RCT_EXPORT_MODULE()

Expand All @@ -72,6 +74,57 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(minDelta, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(legalLabelInsets, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(region, MKCoordinateRegion)
RCT_CUSTOM_VIEW_PROPERTY(annotations, CLLocationCoordinate2D, RCTMap){
if ([json isKindOfClass:[NSArray class]]){
NSMutableDictionary *pins = [NSMutableDictionary dictionary];
id anObject;
NSEnumerator *enumerator = [json objectEnumerator];
while (anObject = [enumerator nextObject]){
CLLocationCoordinate2D coordinate = [RCTConvert CLLocationCoordinate2D:anObject];
if (CLLocationCoordinate2DIsValid(coordinate)){
NSString *title = @"";
if ([anObject objectForKey:@"title"]){
title = [RCTConvert NSString:[anObject valueForKey:@"title"]];
}
MKPointAnnotation *pin = [[MKPointAnnotation alloc]init];
pin.coordinate = coordinate;
if (title.length){
pin.title = title;
}

NSValue *key = [NSValue valueWithMKCoordinate:pin.coordinate];
[pins setObject:pin forKey:key];
}
}
if (pins.count){
if (!pinAnnotations){
pinAnnotations = [NSMutableDictionary dictionary];
}
NSArray *oldKeys = [pinAnnotations allKeys];
NSArray *newKeys = [pins allKeys];
// Remove objects from dictionary if new set has no same coordinates
// and also remove from Map view
if (oldKeys.count){
NSMutableArray *removeableKeys = [NSMutableArray array];
for (NSValue *oldKey in oldKeys){
if (![newKeys containsObject:oldKey]){
[removeableKeys addObject:oldKey];
}
}
// remove keys that are already existing and added onto maps
[pins removeObjectsForKeys:[pinAnnotations allKeys]];
if (removeableKeys.count){
NSArray *removed = [pinAnnotations objectsForKeys:removeableKeys notFoundMarker:[NSNull null]];
[view removeAnnotations: removed];
[pins removeObjectsForKeys:removeableKeys];
[pinAnnotations removeObjectsForKeys:removeableKeys];
}
}
[pinAnnotations addEntriesFromDictionary:pins];
[view addAnnotations:[pinAnnotations allValues]];
}
}
}

#pragma mark MKMapViewDelegate

Expand Down