This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathRNGeocoderModule.java
More file actions
148 lines (120 loc) · 4.98 KB
/
RNGeocoderModule.java
File metadata and controls
148 lines (120 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.devfd.RNGeocoder;
import android.location.Address;
import android.location.Geocoder;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import java.io.IOException;
import java.util.List;
public class RNGeocoderModule extends ReactContextBaseJavaModule {
private Geocoder geocoder;
public RNGeocoderModule(ReactApplicationContext reactContext) {
super(reactContext);
geocoder = new Geocoder(reactContext.getApplicationContext());
}
@Override
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()) {
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 geocodePosition(ReadableMap position, 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);
}
}
WritableArray transform(List<Address> addresses) {
WritableArray results = new WritableNativeArray();
for (Address address: addresses) {
WritableMap result = new WritableNativeMap();
WritableMap position = new WritableNativeMap();
position.putDouble("lat", address.getLatitude());
position.putDouble("lng", address.getLongitude());
result.putMap("position", position);
final String feature_name = address.getFeatureName();
if (feature_name != null && !feature_name.equals(address.getSubThoroughfare()) &&
!feature_name.equals(address.getThoroughfare()) &&
!feature_name.equals(address.getLocality())) {
result.putString("feature", feature_name);
}
else {
result.putString("feature", null);
}
result.putString("locality", address.getLocality());
result.putString("adminArea", address.getAdminArea());
result.putString("country", address.getCountryName());
result.putString("countryCode", address.getCountryCode());
result.putString("locale", address.getLocale().toString());
result.putString("postalCode", address.getPostalCode());
result.putString("subAdminArea", address.getSubAdminArea());
result.putString("subLocality", address.getSubLocality());
result.putString("streetNumber", address.getSubThoroughfare());
result.putString("streetName", address.getThoroughfare());
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(address.getAddressLine(i));
}
result.putString("formattedAddress", sb.toString());
results.pushMap(result);
}
return results;
}
}