@capgo/nativegeocoder 0.1.7 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/ee/forgr/{plugin/nativegeocoder → capacitor_nativegeocoder}/NativeGeocoder.java +39 -37
- package/android/src/main/java/ee/forgr/capacitor_nativegeocoder/NativeGeocoderPlugin.java +38 -0
- package/ios/Plugin/NativeGeocoder.swift +2 -2
- package/package.json +1 -1
- package/android/src/main/java/ee/forgr/plugin/nativegeocoder/NativeGeocoderPlugin.java +0 -39
|
@@ -1,9 +1,30 @@
|
|
|
1
1
|
package ee.forgr.capacitor_nativegeocoder;
|
|
2
2
|
|
|
3
|
-
import android.
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.location.Address;
|
|
5
|
+
import android.location.Geocoder;
|
|
6
|
+
import android.net.ConnectivityManager;
|
|
7
|
+
import android.net.NetworkInfo;
|
|
8
|
+
import android.os.Build;
|
|
9
|
+
|
|
10
|
+
import com.getcapacitor.JSObject;
|
|
11
|
+
import com.getcapacitor.PluginCall;
|
|
12
|
+
|
|
13
|
+
import org.json.JSONArray;
|
|
14
|
+
import org.json.JSONObject;
|
|
15
|
+
|
|
16
|
+
import java.util.List;
|
|
17
|
+
import java.util.Locale;
|
|
18
|
+
|
|
19
|
+
class NativeGeocoderOptions {
|
|
20
|
+
boolean useLocale = true;
|
|
21
|
+
String defaultLocale = null;
|
|
22
|
+
int maxResults = 1;
|
|
23
|
+
}
|
|
4
24
|
|
|
5
25
|
public class NativeGeocoder {
|
|
6
26
|
|
|
27
|
+
private Geocoder geocoder;
|
|
7
28
|
public Context context;
|
|
8
29
|
/**
|
|
9
30
|
* Reverse geocode a given latitude and longitude to find location address
|
|
@@ -11,7 +32,7 @@ public class NativeGeocoder {
|
|
|
11
32
|
* @param longitude double
|
|
12
33
|
* @param call PluginCall
|
|
13
34
|
*/
|
|
14
|
-
|
|
35
|
+
public void reverseGeocode(double latitude, double longitude, PluginCall call) {
|
|
15
36
|
|
|
16
37
|
if (latitude == 0 || longitude == 0) {
|
|
17
38
|
call.reject("Expected two non-empty double arguments.");
|
|
@@ -23,7 +44,7 @@ public class NativeGeocoder {
|
|
|
23
44
|
return;
|
|
24
45
|
}
|
|
25
46
|
|
|
26
|
-
NativeGeocoderOptions geocoderOptions = getNativeGeocoderOptions(
|
|
47
|
+
NativeGeocoderOptions geocoderOptions = getNativeGeocoderOptions(call);
|
|
27
48
|
geocoder = createGeocoderWithOptions(geocoderOptions);
|
|
28
49
|
|
|
29
50
|
try {
|
|
@@ -52,7 +73,9 @@ public class NativeGeocoder {
|
|
|
52
73
|
|
|
53
74
|
resultObj.put(placemark);
|
|
54
75
|
}
|
|
55
|
-
|
|
76
|
+
JSObject ret = new JSObject();
|
|
77
|
+
ret.put("addresses", resultObj);
|
|
78
|
+
call.resolve(ret);
|
|
56
79
|
} else {
|
|
57
80
|
call.reject("Cannot get an address.");
|
|
58
81
|
}
|
|
@@ -70,10 +93,9 @@ public class NativeGeocoder {
|
|
|
70
93
|
/**
|
|
71
94
|
* Forward geocode a given address to find coordinates
|
|
72
95
|
* @param addressString String
|
|
73
|
-
* @param options JSONObject
|
|
74
96
|
* @param call PluginCall
|
|
75
97
|
*/
|
|
76
|
-
|
|
98
|
+
public void forwardGeocode(String addressString, PluginCall call) {
|
|
77
99
|
if (addressString == null || addressString.length() == 0) {
|
|
78
100
|
call.reject("Expected a non-empty string argument.");
|
|
79
101
|
return;
|
|
@@ -103,7 +125,7 @@ public class NativeGeocoder {
|
|
|
103
125
|
|
|
104
126
|
if (!latitude.isEmpty() && !longitude.isEmpty()) {
|
|
105
127
|
// https://developer.android.com/reference/android/location/Address.html
|
|
106
|
-
|
|
128
|
+
JSObject placemark = new JSObject();
|
|
107
129
|
placemark.put("latitude", latitude);
|
|
108
130
|
placemark.put("longitude", longitude);
|
|
109
131
|
placemark.put("countryCode", address.getCountryCode() != null ? address.getCountryCode() : "");
|
|
@@ -128,7 +150,9 @@ public class NativeGeocoder {
|
|
|
128
150
|
if (resultObj.length() == 0) {
|
|
129
151
|
call.reject("Cannot get latitude and/or longitude.");
|
|
130
152
|
} else {
|
|
131
|
-
|
|
153
|
+
JSObject ret = new JSObject();
|
|
154
|
+
ret.put("addresses", resultObj);
|
|
155
|
+
call.resolve(ret);
|
|
132
156
|
}
|
|
133
157
|
|
|
134
158
|
} else {
|
|
@@ -163,38 +187,16 @@ public class NativeGeocoder {
|
|
|
163
187
|
* @param options JSONObject
|
|
164
188
|
* @return NativeGeocoderOptions
|
|
165
189
|
*/
|
|
166
|
-
private NativeGeocoderOptions getNativeGeocoderOptions(
|
|
190
|
+
private NativeGeocoderOptions getNativeGeocoderOptions(PluginCall options) {
|
|
167
191
|
NativeGeocoderOptions geocoderOptions = new NativeGeocoderOptions();
|
|
168
192
|
|
|
169
193
|
if (options != null) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
if (options.has("defaultLocale")) {
|
|
177
|
-
try {
|
|
178
|
-
geocoderOptions.defaultLocale = options.getString("defaultLocale");
|
|
179
|
-
} catch (JSONException e) {
|
|
180
|
-
geocoderOptions.defaultLocale = null;
|
|
181
|
-
}
|
|
182
|
-
} else {
|
|
183
|
-
geocoderOptions.defaultLocale = null;
|
|
184
|
-
}
|
|
185
|
-
if (options.has("maxResults")) {
|
|
186
|
-
try {
|
|
187
|
-
geocoderOptions.maxResults = options.getInt("maxResults");
|
|
188
|
-
} catch (JSONException e) {
|
|
189
|
-
geocoderOptions.maxResults = 1;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (geocoderOptions.maxResults > 0) {
|
|
193
|
-
int MAX_RESULTS_COUNT = 5;
|
|
194
|
-
geocoderOptions.maxResults = Math.min(geocoderOptions.maxResults, MAX_RESULTS_COUNT);
|
|
195
|
-
} else {
|
|
196
|
-
geocoderOptions.maxResults = 1;
|
|
197
|
-
}
|
|
194
|
+
geocoderOptions.useLocale = options.getBoolean("useLocale", false);
|
|
195
|
+
geocoderOptions.defaultLocale = options.getString("defaultLocale", null);
|
|
196
|
+
geocoderOptions.maxResults = options.getInt("maxResults", 1);
|
|
197
|
+
if (geocoderOptions.maxResults > 0) {
|
|
198
|
+
int MAX_RESULTS_COUNT = 5;
|
|
199
|
+
geocoderOptions.maxResults = Math.min(geocoderOptions.maxResults, MAX_RESULTS_COUNT);
|
|
198
200
|
} else {
|
|
199
201
|
geocoderOptions.maxResults = 1;
|
|
200
202
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
package ee.forgr.capacitor_nativegeocoder;
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.Plugin;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import com.getcapacitor.PluginMethod;
|
|
6
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
7
|
+
|
|
8
|
+
@CapacitorPlugin(name = "NativeGeocoder")
|
|
9
|
+
public class NativeGeocoderPlugin extends Plugin {
|
|
10
|
+
|
|
11
|
+
private NativeGeocoder implementation = new NativeGeocoder();
|
|
12
|
+
|
|
13
|
+
@Override
|
|
14
|
+
public void load() {
|
|
15
|
+
super.load();
|
|
16
|
+
implementation.context = this.getContext();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@PluginMethod
|
|
20
|
+
public void reverseGeocode(PluginCall call) {
|
|
21
|
+
Double latitude = call.getDouble("latitude");
|
|
22
|
+
Double longitude = call.getDouble("longitude");
|
|
23
|
+
if (latitude == null || longitude == null) {
|
|
24
|
+
call.reject("Missing latitude or longitude");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
implementation.reverseGeocode(latitude, longitude, call);
|
|
28
|
+
}
|
|
29
|
+
@PluginMethod
|
|
30
|
+
public void forwardGeocode(PluginCall call) {
|
|
31
|
+
String addressString = call.getString("addressString");
|
|
32
|
+
if (addressString == null) {
|
|
33
|
+
call.reject("Missing addressString");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
implementation.forwardGeocode(addressString, call);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -34,7 +34,7 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
34
34
|
if let error = error {
|
|
35
35
|
call.reject(error.message)
|
|
36
36
|
} else {
|
|
37
|
-
call.resolve(["
|
|
37
|
+
call.resolve(["addresses": resultObj])
|
|
38
38
|
}
|
|
39
39
|
})
|
|
40
40
|
}
|
|
@@ -104,7 +104,7 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
104
104
|
if let error = error {
|
|
105
105
|
call.reject(error.message)
|
|
106
106
|
} else {
|
|
107
|
-
call.resolve(["
|
|
107
|
+
call.resolve(["addresses": resultObj])
|
|
108
108
|
}
|
|
109
109
|
})
|
|
110
110
|
}
|
package/package.json
CHANGED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
package ee.forgr.capacitor_nativegeocoder;
|
|
2
|
-
|
|
3
|
-
import com.getcapacitor.JSObject;
|
|
4
|
-
import com.getcapacitor.Plugin;
|
|
5
|
-
import com.getcapacitor.PluginCall;
|
|
6
|
-
import com.getcapacitor.PluginMethod;
|
|
7
|
-
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
8
|
-
|
|
9
|
-
@CapacitorPlugin(name = "NativeGeocoder")
|
|
10
|
-
public class NativeGeocoderPlugin extends Plugin {
|
|
11
|
-
|
|
12
|
-
private NativeGeocoder implementation = new NativeGeocoder();
|
|
13
|
-
|
|
14
|
-
@Override
|
|
15
|
-
public void load() {
|
|
16
|
-
super.load();
|
|
17
|
-
implementation.context = this.getContext()
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
@PluginMethod
|
|
21
|
-
public void reverseGeocode(PluginCall call) {
|
|
22
|
-
String addressString = call.getString("addressString");
|
|
23
|
-
if (addressString == null) {
|
|
24
|
-
call.reject("Missing addressString");
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
implementation.reverseGeocode(addressString, call)
|
|
28
|
-
}
|
|
29
|
-
@PluginMethod
|
|
30
|
-
public void forwardGeocode(PluginCall call) {
|
|
31
|
-
Number latitude = call.getDouble("latitude");
|
|
32
|
-
Number longitude = call.getDouble("longitude");
|
|
33
|
-
if (latitude == null || longitude == null) {
|
|
34
|
-
call.reject("Missing latitude or longitude");
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
implementation.forwardGeocode(latitude, longitude, call)
|
|
38
|
-
}
|
|
39
|
-
}
|