|
3 | 3 | import android.location.Address; |
4 | 4 | import android.location.Geocoder; |
5 | 5 |
|
6 | | -import com.facebook.react.bridge.Callback; |
7 | | -import com.facebook.react.bridge.NativeModule; |
| 6 | +import com.facebook.react.bridge.Promise; |
8 | 7 | import com.facebook.react.bridge.ReactApplicationContext; |
9 | | -import com.facebook.react.bridge.ReactContext; |
10 | 8 | import com.facebook.react.bridge.ReactContextBaseJavaModule; |
11 | 9 | import com.facebook.react.bridge.ReactMethod; |
12 | | -import com.facebook.react.bridge.ReadableArray; |
13 | 10 | import com.facebook.react.bridge.ReadableMap; |
14 | 11 | import com.facebook.react.bridge.WritableArray; |
15 | 12 | import com.facebook.react.bridge.WritableMap; |
16 | 13 | import com.facebook.react.bridge.WritableNativeArray; |
17 | 14 | import com.facebook.react.bridge.WritableNativeMap; |
18 | 15 |
|
19 | | -import org.json.JSONArray; |
20 | | - |
21 | 16 | import java.io.IOException; |
22 | 17 | import java.util.List; |
23 | 18 |
|
24 | 19 | public class RNGeocoderModule extends ReactContextBaseJavaModule { |
25 | 20 |
|
26 | | - public RNGeocoderModule(ReactApplicationContext reactContext) { |
27 | | - super(reactContext); |
28 | | - } |
29 | | - |
30 | | - @Override |
31 | | - public String getName() { |
32 | | - return "RNGeocoder"; |
33 | | - } |
34 | | - |
35 | | - @ReactMethod |
36 | | - public void geocodeAddress(String addressName, Callback errorCallback, Callback successCallback) { |
37 | | - Geocoder geocoder = new Geocoder(getReactApplicationContext()); |
38 | | - try { |
39 | | - List<Address> addresses = geocoder.getFromLocationName(addressName, 20); |
40 | | - successCallback.invoke(transform(addresses)); |
41 | | - } |
42 | | - catch (IOException e) { |
43 | | - errorCallback.invoke(e.getMessage()); |
44 | | - } |
45 | | - } |
| 21 | + private Geocoder geocoder; |
46 | 22 |
|
47 | | - @ReactMethod |
48 | | - public void reverseGeocodeLocation(ReadableMap position, Callback errorCallback, Callback successCallback) { |
49 | | - Geocoder geocoder = new Geocoder(getReactApplicationContext()); |
| 23 | + public RNGeocoderModule(ReactApplicationContext reactContext) { |
| 24 | + super(reactContext); |
| 25 | + geocoder = new Geocoder(reactContext.getApplicationContext()); |
| 26 | + } |
50 | 27 |
|
51 | | - try { |
52 | | - List<Address> addresses = geocoder.getFromLocation(position.getDouble("latitude"), position.getDouble("longitude"), 20); |
53 | | - successCallback.invoke(transform(addresses)); |
| 28 | + @Override |
| 29 | + public String getName() { |
| 30 | + return "RNGeocoder"; |
54 | 31 | } |
55 | | - catch (IOException e) { |
56 | | - errorCallback.invoke(e.getMessage()); |
| 32 | + |
| 33 | + @ReactMethod |
| 34 | + public void geocodeAddress(String addressName, Promise promise) { |
| 35 | + if (!geocoder.isPresent()) { |
| 36 | + promise.reject("-1", "Geocoder not available for this platform"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + List<Address> addresses = geocoder.getFromLocationName(addressName, 20); |
| 42 | + promise.resolve(transform(addresses)); |
| 43 | + } |
| 44 | + |
| 45 | + catch (IOException e) { |
| 46 | + promise.reject(e); |
| 47 | + } |
57 | 48 | } |
58 | 49 |
|
59 | | - } |
60 | | - |
61 | | - WritableArray transform(List<Address> addresses) { |
62 | | - WritableArray results = new WritableNativeArray(); |
63 | | - |
64 | | - for (Address address: addresses) { |
65 | | - WritableMap result = new WritableNativeMap(); |
66 | | - |
67 | | - WritableMap position = new WritableNativeMap(); |
68 | | - position.putDouble("lat", address.getLatitude()); |
69 | | - position.putDouble("lng", address.getLongitude()); |
70 | | - result.putMap("position", position); |
71 | | - |
72 | | - result.putString("locality", address.getLocality()); |
73 | | - result.putString("adminArea", address.getAdminArea()); |
74 | | - result.putString("country", address.getCountryName()); |
75 | | - result.putString("countryCode", address.getCountryCode()); |
76 | | - result.putString("locale", address.getLocale().toString()); |
77 | | - result.putString("postalCode", address.getPostalCode()); |
78 | | - result.putString("subAdminArea", address.getSubAdminArea()); |
79 | | - result.putString("subLocality", address.getSubLocality()); |
80 | | - result.putString("subThoroughfare", address.getSubThoroughfare()); |
81 | | - result.putString("thoroughfare", address.getThoroughfare()); |
82 | | - results.pushMap(result); |
| 50 | + @ReactMethod |
| 51 | + public void geocodePosition(ReadableMap position, Promise promise) { |
| 52 | + if (!geocoder.isPresent()) { |
| 53 | + promise.reject("-1", "Geocoder not available for this platform"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + List<Address> addresses = geocoder.getFromLocation(position.getDouble("lat"), position.getDouble("lng"), 20); |
| 59 | + promise.resolve(transform(addresses)); |
| 60 | + } |
| 61 | + catch (IOException e) { |
| 62 | + promise.reject(e); |
| 63 | + } |
83 | 64 | } |
84 | 65 |
|
85 | | - return results; |
86 | | - } |
| 66 | + WritableArray transform(List<Address> addresses) { |
| 67 | + WritableArray results = new WritableNativeArray(); |
| 68 | + |
| 69 | + for (Address address: addresses) { |
| 70 | + WritableMap result = new WritableNativeMap(); |
| 71 | + |
| 72 | + WritableMap position = new WritableNativeMap(); |
| 73 | + position.putDouble("lat", address.getLatitude()); |
| 74 | + position.putDouble("lng", address.getLongitude()); |
| 75 | + result.putMap("position", position); |
| 76 | + |
| 77 | + if (!address.getFeatureName().equals(address.getSubThoroughfare()) && |
| 78 | + !address.getFeatureName().equals(address.getThoroughfare()) && |
| 79 | + !address.getFeatureName().equals(address.getLocality())) { |
| 80 | + |
| 81 | + result.putString("feature", address.getFeatureName()); |
| 82 | + } |
| 83 | + else { |
| 84 | + result.putString("feature", null); |
| 85 | + } |
| 86 | + |
| 87 | + result.putString("locality", address.getLocality()); |
| 88 | + result.putString("adminArea", address.getAdminArea()); |
| 89 | + result.putString("country", address.getCountryName()); |
| 90 | + result.putString("countryCode", address.getCountryCode()); |
| 91 | + result.putString("locale", address.getLocale().toString()); |
| 92 | + result.putString("postalCode", address.getPostalCode()); |
| 93 | + result.putString("subAdminArea", address.getSubAdminArea()); |
| 94 | + result.putString("subLocality", address.getSubLocality()); |
| 95 | + result.putString("streetNumber", address.getSubThoroughfare()); |
| 96 | + result.putString("streetName", address.getThoroughfare()); |
| 97 | + |
| 98 | + StringBuilder sb = new StringBuilder(); |
| 99 | + |
| 100 | + for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) { |
| 101 | + if (i > 0) { |
| 102 | + sb.append(','); |
| 103 | + } |
| 104 | + sb.append(address.getAddressLine(i)); |
| 105 | + } |
| 106 | + |
| 107 | + result.putString("formattedAddress", sb.toString()); |
| 108 | + |
| 109 | + results.pushMap(result); |
| 110 | + } |
| 111 | + |
| 112 | + return results; |
| 113 | + } |
87 | 114 | } |
0 commit comments