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 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 11 additions & 5 deletions android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down
40 changes: 29 additions & 11 deletions ios/RNGeocoder/RNGeocoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -23,6 +24,7 @@ @implementation RNGeocoder
RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(geocodePosition:(CLLocation *)location
NSString: locale
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions js/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down