diff --git a/README.md b/README.md index 23e2ef6..7455459 100644 --- a/README.md +++ b/README.md @@ -99,10 +99,17 @@ Geocoder.fallbackToGoogle(MY_KEY); // use the lib as usual let ret = await Geocoder.geocodePosition({lat, lng}) + +const locale = 'en'; // Two symbol locale code. +let ret = await Geocoder.geocodePosition({lat, lng}, locale); + // you get the same results ``` +By default `locale` is 'en'. + + ## With async / await ``` try { diff --git a/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java b/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java index 642ebfb..10e56c2 100644 --- a/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java +++ b/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java @@ -15,14 +15,15 @@ import java.io.IOException; import java.util.List; +import java.util.Locale; public class RNGeocoderModule extends ReactContextBaseJavaModule { - private Geocoder geocoder; + private ReactApplicationContext reactContext; - public RNGeocoderModule(ReactApplicationContext reactContext) { - super(reactContext); - geocoder = new Geocoder(reactContext.getApplicationContext()); + public RNGeocoderModule(ReactApplicationContext context) { + super(context); + reactContext = context; } @Override @@ -32,6 +33,8 @@ public String getName() { @ReactMethod public void geocodeAddress(String addressName, Promise promise) { + Geocoder geocoder = new Geocoder(reactContext.getApplicationContext()); + if (!geocoder.isPresent()) { promise.reject("NOT_AVAILABLE", "Geocoder not available for this platform"); return; @@ -48,7 +51,10 @@ public void geocodeAddress(String addressName, Promise promise) { } @ReactMethod - public void geocodePosition(ReadableMap position, Promise promise) { + public void geocodePosition(ReadableMap position, String locale, Promise promise) { + Locale geocoderLocale = new Locale(locale); + Geocoder geocoder = new Geocoder(reactContext.getApplicationContext(), geocoderLocale); + if (!geocoder.isPresent()) { promise.reject("NOT_AVAILABLE", "Geocoder not available for this platform"); return; diff --git a/ios/RNGeocoder/RNGeocoder.m b/ios/RNGeocoder/RNGeocoder.m index 1a54347..313f4ba 100644 --- a/ios/RNGeocoder/RNGeocoder.m +++ b/ios/RNGeocoder/RNGeocoder.m @@ -12,6 +12,7 @@ + (CLLocation *)CLLocation:(id)json double lat = [RCTConvert double:json[@"lat"]]; double lng = [RCTConvert double:json[@"lng"]]; + return [[CLLocation alloc] initWithLatitude:lat longitude:lng]; } @@ -23,6 +24,7 @@ @implementation RNGeocoder RCT_EXPORT_MODULE(); RCT_EXPORT_METHOD(geocodePosition:(CLLocation *)location + NSString: locale resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { @@ -34,19 +36,35 @@ @implementation RNGeocoder [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); - } + NSLocale *geocoderLocale = [[NSLocale alloc] initWithLocaleIdentifier:locale]; - return reject(@"ERROR", @"geocodePosition failed", error); + if (@available(iOS 11.0, *)) { + [self.geocoder reverseGeocodeLocation:location preferredLocale:geocoderLocale 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]); + + }]; + } else { + [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]); + + }]; } - - resolve([self placemarksToDictionary:placemarks]); - - }]; } RCT_EXPORT_METHOD(geocodeAddress:(NSString *)address diff --git a/js/geocoder.js b/js/geocoder.js index 3a4563e..575503a 100644 --- a/js/geocoder.js +++ b/js/geocoder.js @@ -10,12 +10,12 @@ export default { this.apiKey = key; }, - geocodePosition(position) { + geocodePosition(position, locale = "en") { if (!position || !position.lat || !position.lng) { return Promise.reject(new Error("invalid position: {lat, lng} required")); } - return RNGeocoder.geocodePosition(position).catch(err => { + return RNGeocoder.geocodePosition(position, locale).catch(err => { if (!this.apiKey) { throw err; } return GoogleApi.geocodePosition(this.apiKey, position); });