Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Proposal to add language parameter to get result on preferred language. (iOS only for now, but already added the function in android as well ) #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ public String getName() {
return "RNGeocoder";
}

@ReactMethod
public void geocodeAddressWithLanguage(String addressName, String language, Promise promise) {
if (!geocoder.isPresent()) {
promise.reject("NOT_AVAILABLE", "Geocoder not available for this platform");
return;
}

try {
List<Address> addresses = geocoder.getFromLocationName(addressName, 20);
promise.resolve(transform(addresses));
}

catch (IOException e) {
promise.reject(e);
}
}

@ReactMethod
public void geocodePositionWithLanguage(ReadableMap position, String language, Promise promise) {
if (!geocoder.isPresent()) {
promise.reject("NOT_AVAILABLE", "Geocoder not available for this platform");
return;
}

try {
List<Address> addresses = geocoder.getFromLocation(position.getDouble("lat"), position.getDouble("lng"), 20);
promise.resolve(transform(addresses));
}
catch (IOException e) {
promise.reject(e);
}
}

@ReactMethod
public void geocodeAddress(String addressName, Promise promise) {
if (!geocoder.isPresent()) {
Expand Down
1 change: 1 addition & 0 deletions ios/RNGeocoder/RNGeocoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

@interface RNGeocoder : NSObject<RCTBridgeModule>
@property (nonatomic, strong) CLGeocoder *geocoder;
@property (nonatomic, strong) NSString *oldLanguage;
@end
223 changes: 151 additions & 72 deletions ios/RNGeocoder/RNGeocoder.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#import "RNGeocoder.h"

#import <CoreLocation/CoreLocation.h>

#import <React/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 @@ -22,31 +21,104 @@ @implementation RNGeocoder

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(geocodePosition:(CLLocation *)location

RCT_EXPORT_METHOD(geocodePositionWithLanguage:(CLLocation *)location

language:(NSString *)language

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);
}
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}

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

[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"AppleLanguages"];
NSString * oldLang = [[NSLocale preferredLanguages] objectAtIndex:0];
self.oldLanguage = oldLang;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:language, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

[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]);

}];
}

return reject(@"ERROR", @"geocodePosition failed", error);
RCT_EXPORT_METHOD(geocodeAddressWithLanguage:(NSString *)address

language:(NSString *)language

resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}

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

[[NSUserDefaults standardUserDefaults]removeObjectForKey:@"AppleLanguages"];
NSString * oldLang = [[NSLocale preferredLanguages] objectAtIndex:0];
self.oldLanguage = oldLang;
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:language, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

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

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

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

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

resolve([self placemarksToDictionary:placemarks]);

}];
RCT_EXPORT_METHOD(geocodePosition:(CLLocation *)location
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);
}

resolve([self placemarksToDictionary:placemarks]);

}];
}

RCT_EXPORT_METHOD(geocodeAddress:(NSString *)address
Expand All @@ -56,67 +128,74 @@ @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;



if(self.oldLanguage){
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:self.oldLanguage, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.oldLanguage = nil;
}

return results;

}

@end
22 changes: 22 additions & 0 deletions js/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ export default {
this.apiKey = key;
},

geocodePositionWithLanguage(position, language){
if (!position || !position.lat || !position.lng) {
return Promise.reject(new Error("invalid position: {lat, lng} required"));
}

return RNGeocoder.geocodePositionWithLanguage(position, language).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodePosition(this.apiKey, position);
});
}

Choose a reason for hiding this comment

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

There is a missing , on this line which causes an unexpected token error


geocodeAddressWithLanguage(address, language) {
if (!address) {
return Promise.reject(new Error("address is null"));
}

return RNGeocoder.geocodeAddressWithLanguage(address, language).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodeAddress(this.apiKey, address);
});
},

geocodePosition(position) {
if (!position || !position.lat || !position.lng) {
return Promise.reject(new Error("invalid position: {lat, lng} required"));
Expand Down