-
Notifications
You must be signed in to change notification settings - Fork 7
/
RNLocalSearch.m
60 lines (44 loc) · 1.92 KB
/
RNLocalSearch.m
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
// RNLocalSearch.m
#import "RNLocalSearch.h"
#import "RCTUtils.h"
@implementation RNLocalSearch
{
MKLocalSearch *localSearch;
}
RCT_EXPORT_MODULE()
#pragma mark -
#pragma mark format RCT callback response
- (NSArray *)formatLocalSearchCallback:(MKLocalSearchResponse *)localSearchResponse
{
NSMutableArray *RCTResponse = [[NSMutableArray alloc] init];
for (MKMapItem *mapItem in localSearchResponse.mapItems) {
NSMutableDictionary *formedLocation = [[NSMutableDictionary alloc] init];
[formedLocation setValue:mapItem.name forKey:@"name"];
[formedLocation setValue:mapItem.placemark.title forKey:@"title"];
[formedLocation setValue:mapItem.phoneNumber forKey:@"phoneNumber"];
[formedLocation setValue:@{@"latitude": @(mapItem.placemark.coordinate.latitude),
@"longitude": @(mapItem.placemark.coordinate.longitude)} forKey:@"location"];
[RCTResponse addObject:formedLocation];
}
return [RCTResponse copy];
}
#pragma mark -
#pragma mark RCT Exports
RCT_EXPORT_METHOD(searchForLocations:(NSString *)searchText near:(MKCoordinateRegion)region callback:(RCTResponseSenderBlock)callback)
{
[localSearch cancel];
MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
searchRequest.naturalLanguageQuery = searchText;
searchRequest.region = region;
localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
__weak RNLocalSearch *weakSelf = self;
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
if (error) {
callback(@[RCTMakeError(@"Failed to make local search. ", error, @{@"key": searchText}), [NSNull null]]);
} else {
NSArray *RCTResponse = [weakSelf formatLocalSearchCallback:response];
callback(@[[NSNull null], RCTResponse]);
}
}];
}
@end