@capgo/nativegeocoder 0.1.8 → 0.1.9

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
@@ -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 {
@@ -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,7 +93,6 @@ 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
  private void forwardGeocode(String addressString, PluginCall call) {
@@ -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
- JSONObject placemark = new JSONObject();
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
- 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
  }
@@ -1,6 +1,5 @@
1
1
  package ee.forgr.capacitor_nativegeocoder;
2
2
 
3
- import com.getcapacitor.JSObject;
4
3
  import com.getcapacitor.Plugin;
5
4
  import com.getcapacitor.PluginCall;
6
5
  import com.getcapacitor.PluginMethod;
@@ -14,7 +13,7 @@ public class NativeGeocoderPlugin extends Plugin {
14
13
  @Override
15
14
  public void load() {
16
15
  super.load();
17
- implementation.context = this.getContext()
16
+ implementation.context = this.getContext();
18
17
  }
19
18
 
20
19
  @PluginMethod
@@ -24,7 +23,7 @@ public class NativeGeocoderPlugin extends Plugin {
24
23
  call.reject("Missing addressString");
25
24
  return;
26
25
  }
27
- implementation.reverseGeocode(addressString, call)
26
+ implementation.reverseGeocode(addressString, call);
28
27
  }
29
28
  @PluginMethod
30
29
  public void forwardGeocode(PluginCall call) {
@@ -34,6 +33,6 @@ public class NativeGeocoderPlugin extends Plugin {
34
33
  call.reject("Missing latitude or longitude");
35
34
  return;
36
35
  }
37
- implementation.forwardGeocode(latitude, longitude, call)
36
+ implementation.forwardGeocode(latitude, longitude, call);
38
37
  }
39
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.9",
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",