Skip to content

Commit

Permalink
Fix for pointForCoordinate and coordinateForPoint (#2039)
Browse files Browse the repository at this point in the history
* Fix for pointForCoordinate and coordinateForPoint

* reject when an invalid OS was passed
  • Loading branch information
wasedaigo authored and rborn committed Mar 8, 2018
1 parent 6bd1739 commit 5c694d6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 64 deletions.
40 changes: 8 additions & 32 deletions lib/components/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,18 +658,7 @@ class MapView extends React.Component {
if (Platform.OS === 'android') {
return NativeModules.AirMapModule.pointForCoordinate(this._getHandle(), coordinate);
} else if (Platform.OS === 'ios') {
return new Promise((resolve, reject) => {
this._runCommand('pointForCoordinate', [
coordinate,
(err, snapshot) => {
if (err) {
reject(err);
} else {
resolve(snapshot);
}
},
]);
});
return this._runCommand('pointForCoordinate', [coordinate]);
}
return Promise.reject('pointForCoordinate not supported on this platform');
}
Expand All @@ -683,24 +672,13 @@ class MapView extends React.Component {
*
* @return Promise Promise with the coordinate ({ latitude: Number, longitude: Number })
*/
coordinateFromPoint(point) {
coordinateForPoint(point) {
if (Platform.OS === 'android') {
return NativeModules.AirMapModule.coordinateFromPoint(this._getHandle(), point);
return NativeModules.AirMapModule.coordinateForPoint(this._getHandle(), point);
} else if (Platform.OS === 'ios') {
return new Promise((resolve, reject) => {
this._runCommand('coordinateFromPoint', [
point,
(err, snapshot) => {
if (err) {
reject(err);
} else {
resolve(snapshot);
}
},
]);
});
return this._runCommand('coordinateForPoint', [point]);
}
return Promise.reject('coordinateFromPoint not supported on this platform');
return Promise.reject('coordinateForPoint not supported on this platform');
}

_uiManagerCommand(name) {
Expand All @@ -718,19 +696,17 @@ class MapView extends React.Component {
_runCommand(name, args) {
switch (Platform.OS) {
case 'android':
NativeModules.UIManager.dispatchViewManagerCommand(
return NativeModules.UIManager.dispatchViewManagerCommand(
this._getHandle(),
this._uiManagerCommand(name),
args
);
break;

case 'ios':
this._mapManagerCommand(name)(this._getHandle(), ...args);
break;
return this._mapManagerCommand(name)(this._getHandle(), ...args);

default:
break;
return Promise.reject(`Invalid platform was passed: ${Platform.OS}`);
}
}

Expand Down
32 changes: 0 additions & 32 deletions lib/ios/AirMaps/AIRMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -316,38 +316,6 @@ - (void)setLoadingIndicatorColor:(UIColor *)loadingIndicatorColor {
self.activityIndicatorView.color = loadingIndicatorColor;
}

RCT_EXPORT_METHOD(pointForCoordinate:(NSDictionary *)coordinate resolver: (RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
CGPoint touchPoint = [self convertCoordinate:
CLLocationCoordinate2DMake(
[coordinate[@"lat"] doubleValue],
[coordinate[@"lng"] doubleValue]
)
toPointToView:self];

resolve(@{
@"x": @(touchPoint.x),
@"y": @(touchPoint.y),
});
}

RCT_EXPORT_METHOD(coordinateForPoint:(NSDictionary *)point resolver: (RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
CLLocationCoordinate2D coordinate = [self convertPoint:
CGPointMake(
[point[@"x"] doubleValue],
[point[@"y"] doubleValue]
)
toCoordinateFromView:self];

resolve(@{
@"lat": @(coordinate.latitude),
@"lng": @(coordinate.longitude),
});
}

// Include properties of MKMapView which are only available on iOS 9+
// and check if their selector is available before calling super method.

Expand Down
52 changes: 52 additions & 0 deletions lib/ios/AirMaps/AIRMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,58 @@ - (UIView *)view
}];
}

RCT_EXPORT_METHOD(pointForCoordinate:(nonnull NSNumber *)reactTag
coordinate: (NSDictionary *)coordinate
resolver: (RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
id view = viewRegistry[reactTag];
AIRMap *mapView = (AIRMap *)view;
if (![view isKindOfClass:[AIRMap class]]) {
reject(@"Invalid argument", [NSString stringWithFormat:@"Invalid view returned from registry, expecting AIRMap, got: %@", view], NULL);
} else {
CGPoint touchPoint = [mapView convertCoordinate:
CLLocationCoordinate2DMake(
[coordinate[@"lat"] doubleValue],
[coordinate[@"lng"] doubleValue]
)
toPointToView:mapView];

resolve(@{
@"x": @(touchPoint.x),
@"y": @(touchPoint.y),
});
}
}];
}

RCT_EXPORT_METHOD(coordinateForPoint:(nonnull NSNumber *)reactTag
point:(NSDictionary *)point
resolver: (RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
id view = viewRegistry[reactTag];
AIRMap *mapView = (AIRMap *)view;
if (![view isKindOfClass:[AIRMap class]]) {
reject(@"Invalid argument", [NSString stringWithFormat:@"Invalid view returned from registry, expecting AIRMap, got: %@", view], NULL);
} else {
CLLocationCoordinate2D coordinate = [mapView convertPoint:
CGPointMake(
[point[@"x"] doubleValue],
[point[@"y"] doubleValue]
)
toCoordinateFromView:mapView];

resolve(@{
@"lat": @(coordinate.latitude),
@"lng": @(coordinate.longitude),
});
}
}];
}

#pragma mark Take Snapshot
- (void)takeMapSnapshot:(AIRMap *)mapView
snapshotter:(MKMapSnapshotter *) snapshotter
Expand Down

0 comments on commit 5c694d6

Please sign in to comment.