@capgo/nativegeocoder 0.1.8 → 0.1.11

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.
@@ -1,3 +1,4 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
2
  package="ee.forgr.capacitor_nativegeocoder">
3
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3
4
  </manifest>
@@ -1,9 +1,30 @@
1
1
  package ee.forgr.capacitor_nativegeocoder;
2
2
 
3
- import android.util.Log;
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
- private void reverseGeocode(double latitude, double longitude, PluginCall call) {
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(options);
47
+ NativeGeocoderOptions geocoderOptions = getNativeGeocoderOptions(call);
27
48
  geocoder = createGeocoderWithOptions(geocoderOptions);
28
49
 
29
50
  try {
@@ -37,8 +58,8 @@ public class NativeGeocoder {
37
58
 
38
59
  // https://developer.android.com/reference/android/location/Address.html
39
60
  JSONObject placemark = new JSONObject();
40
- placemark.put("latitude", !String.valueOf(address.getLatitude()).isEmpty() ? address.getLatitude() : "");
41
- placemark.put("longitude", !String.valueOf(address.getLongitude()).isEmpty() ? address.getLongitude() : "");
61
+ placemark.put("latitude", !String.valueOf(address.getLatitude()).isEmpty() ? address.getLatitude() : 0);
62
+ placemark.put("longitude", !String.valueOf(address.getLongitude()).isEmpty() ? address.getLongitude() : 0);
42
63
  placemark.put("countryCode", address.getCountryCode() != null ? address.getCountryCode() : "");
43
64
  placemark.put("countryName", address.getCountryName() != null ? address.getCountryName() : "");
44
65
  placemark.put("postalCode", address.getPostalCode() != null ? address.getPostalCode() : "");
@@ -52,7 +73,9 @@ public class NativeGeocoder {
52
73
 
53
74
  resultObj.put(placemark);
54
75
  }
55
- call.resolve(resultObj);
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
- private void forwardGeocode(String addressString, PluginCall call) {
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,9 +125,9 @@ public class NativeGeocoder {
103
125
 
104
126
  if (!latitude.isEmpty() && !longitude.isEmpty()) {
105
127
  // https://developer.android.com/reference/android/location/Address.html
106
- JSONObject placemark = new JSONObject();
107
- placemark.put("latitude", latitude);
108
- placemark.put("longitude", longitude);
128
+ JSObject placemark = new JSObject();
129
+ placemark.put("latitude", address.getLatitude();
130
+ placemark.put("longitude", address.getLongitude());
109
131
  placemark.put("countryCode", address.getCountryCode() != null ? address.getCountryCode() : "");
110
132
  placemark.put("countryName", address.getCountryName() != null ? address.getCountryName() : "");
111
133
  placemark.put("postalCode", address.getPostalCode() != null ? address.getPostalCode() : "");
@@ -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
- call.resolve(resultObj);
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(JSONObject options) {
190
+ private NativeGeocoderOptions getNativeGeocoderOptions(PluginCall options) {
167
191
  NativeGeocoderOptions geocoderOptions = new NativeGeocoderOptions();
168
192
 
169
193
  if (options != null) {
170
- try {
171
- geocoderOptions.useLocale = !options.has("useLocale") || options.getBoolean("useLocale");
172
- } catch (JSONException e) {
173
- geocoderOptions.useLocale = true;
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/nativegeocoder",
3
- "version": "0.1.8",
3
+ "version": "0.1.11",
4
4
  "description": "Capacitor plugin for native forward and reverse geocoding",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -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
- }