Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions ios/RNGeocoder/RNGeocoder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTConvert.h>
#import "RCTBridgeModule.h"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not compile >= RN 0.40

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. will update

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itinance btw i have put the back in latest commit, can u review on the latest commit not this one :)

#import "RCTConvert.h"

#import <CoreLocation/CoreLocation.h>

Expand All @@ -9,4 +9,5 @@

@interface RNGeocoder : NSObject<RCTBridgeModule>
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (nonatomic, strong) NSString *oldLanguage;
@end
155 changes: 81 additions & 74 deletions ios/RNGeocoder/RNGeocoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

#import <CoreLocation/CoreLocation.h>

#import <React/RCTConvert.h>
#import "RCTConvert.h"

@implementation RCTConvert (CoreLocation)

+ (CLLocation *)CLLocation:(id)json
{
json = [self NSDictionary:json];

double lat = [RCTConvert double:json[@"lat"]];
double lng = [RCTConvert double:json[@"lng"]];
return [[CLLocation alloc] initWithLatitude:lat longitude:lng];
json = [self NSDictionary:json];
double lat = [RCTConvert double:json[@"lat"]];
double lng = [RCTConvert double:json[@"lng"]];
return [[CLLocation alloc] initWithLatitude:lat longitude:lng];
}

@end
Expand All @@ -26,27 +26,33 @@ @implementation RNGeocoder
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}

if (self.geocoder.geocoding) {
[self.geocoder cancelGeocode];
}

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

if (error) {
if (placemarks.count == 0) {
return reject(@"NOT_FOUND", @"geocodePosition failed", error);
}

return reject(@"ERROR", @"geocodePosition failed", error);
[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"AppleLanguages"];
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
self.oldLanguage = language;
NSLog(@"OLD LANG %@", self.oldLanguage);
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}

resolve([self placemarksToDictionary:placemarks]);

}];

if (self.geocoder.geocoding) {
[self.geocoder cancelGeocode];
}

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

if (error) {
if (placemarks.count == 0) {
return reject(@"NOT_FOUND", @"geocodePosition failed", error);
}

return reject(@"ERROR", @"geocodePosition failed", error);
}

resolve([self placemarksToDictionary:placemarks]);

}];
}

RCT_EXPORT_METHOD(geocodeAddress:(NSString *)address
Expand All @@ -56,67 +62,68 @@ @implementation RNGeocoder
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}

if (self.geocoder.geocoding) {
[self.geocoder cancelGeocode];
[self.geocoder cancelGeocode];
}

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

if (error) {
if (placemarks.count == 0) {
return reject(@"NOT_FOUND", @"geocodeAddress failed", error);
return reject(@"NOT_FOUND", @"geocodeAddress failed", error);
}

return reject(@"ERROR", @"geocodeAddress failed", error);
}

resolve([self placemarksToDictionary:placemarks]);
}];
}];
}

- (NSArray *)placemarksToDictionary:(NSArray *)placemarks {

NSMutableArray *results = [[NSMutableArray alloc] init];

for (int i = 0; i < placemarks.count; i++) {
CLPlacemark* placemark = [placemarks objectAtIndex:i];

NSString* name = [NSNull null];

if (![placemark.name isEqualToString:placemark.locality] &&
![placemark.name isEqualToString:placemark.thoroughfare] &&
![placemark.name isEqualToString:placemark.subThoroughfare])
{

name = placemark.name;

NSMutableArray *results = [[NSMutableArray alloc] init];

for (int i = 0; i < placemarks.count; i++) {
CLPlacemark* placemark = [placemarks objectAtIndex:i];

NSString* name = [NSNull null];

if (![placemark.name isEqualToString:placemark.locality] &&
![placemark.name isEqualToString:placemark.thoroughfare] &&
![placemark.name isEqualToString:placemark.subThoroughfare])
{

name = placemark.name;
}

NSArray *lines = placemark.addressDictionary[@"FormattedAddressLines"];

NSDictionary *result = @{
@"feature": name,
@"position": @{
@"lat": [NSNumber numberWithDouble:placemark.location.coordinate.latitude],
@"lng": [NSNumber numberWithDouble:placemark.location.coordinate.longitude],
},
@"country": placemark.country ?: [NSNull null],
@"countryCode": placemark.ISOcountryCode ?: [NSNull null],
@"locality": placemark.locality ?: [NSNull null],
@"subLocality": placemark.subLocality ?: [NSNull null],
@"streetName": placemark.thoroughfare ?: [NSNull null],
@"streetNumber": placemark.subThoroughfare ?: [NSNull null],
@"postalCode": placemark.postalCode ?: [NSNull null],
@"adminArea": placemark.administrativeArea ?: [NSNull null],
@"subAdminArea": placemark.subAdministrativeArea ?: [NSNull null],
@"formattedAddress": [lines componentsJoinedByString:@", "]
};

[results addObject:result];
}

NSArray *lines = placemark.addressDictionary[@"FormattedAddressLines"];

NSDictionary *result = @{
@"feature": name,
@"position": @{
@"lat": [NSNumber numberWithDouble:placemark.location.coordinate.latitude],
@"lng": [NSNumber numberWithDouble:placemark.location.coordinate.longitude],
},
@"country": placemark.country ?: [NSNull null],
@"countryCode": placemark.ISOcountryCode ?: [NSNull null],
@"locality": placemark.locality ?: [NSNull null],
@"subLocality": placemark.subLocality ?: [NSNull null],
@"streetName": placemark.thoroughfare ?: [NSNull null],
@"streetNumber": placemark.subThoroughfare ?: [NSNull null],
@"postalCode": placemark.postalCode ?: [NSNull null],
@"adminArea": placemark.administrativeArea ?: [NSNull null],
@"subAdminArea": placemark.subAdministrativeArea ?: [NSNull null],
@"formattedAddress": [lines componentsJoinedByString:@", "]
};

[results addObject:result];
}

return results;

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:self.oldLanguage, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
return results;

}

@end