@capgo/nativegeocoder 0.1.6 → 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.
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/ee/forgr/{plugin/nativegeocoder → capacitor_nativegeocoder}/NativeGeocoder.java +37 -35
- package/android/src/main/java/ee/forgr/{plugin/nativegeocoder → capacitor_nativegeocoder}/NativeGeocoderPlugin.java +3 -4
- package/ios/Plugin/NativeGeocoder.swift +25 -70
- package/package.json +1 -1
|
@@ -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
|
|
@@ -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,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
|
-
|
|
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
|
}
|
|
@@ -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
|
}
|
|
@@ -2,21 +2,6 @@ import Foundation
|
|
|
2
2
|
import Capacitor
|
|
3
3
|
import CoreLocation
|
|
4
4
|
|
|
5
|
-
struct NativeGeocoderResult: Encodable {
|
|
6
|
-
var latitude: String?
|
|
7
|
-
var longitude: String?
|
|
8
|
-
var countryCode: String?
|
|
9
|
-
var countryName: String?
|
|
10
|
-
var postalCode: String?
|
|
11
|
-
var administrativeArea: String?
|
|
12
|
-
var subAdministrativeArea: String?
|
|
13
|
-
var locality: String?
|
|
14
|
-
var subLocality: String?
|
|
15
|
-
var thoroughfare: String?
|
|
16
|
-
var subThoroughfare: String?
|
|
17
|
-
var areasOfInterest: [String]?
|
|
18
|
-
}
|
|
19
|
-
|
|
20
5
|
struct NativeGeocoderError {
|
|
21
6
|
var message: String
|
|
22
7
|
}
|
|
@@ -29,8 +14,8 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
29
14
|
|
|
30
15
|
@objc public class NativeGeocoder: NSObject {
|
|
31
16
|
|
|
32
|
-
typealias ReverseGeocodeCompletionHandler = ([
|
|
33
|
-
typealias ForwardGeocodeCompletionHandler = ([
|
|
17
|
+
typealias ReverseGeocodeCompletionHandler = ([JSObject]?, NativeGeocoderError?) -> Void
|
|
18
|
+
typealias ForwardGeocodeCompletionHandler = ([JSObject]?, NativeGeocoderError?) -> Void
|
|
34
19
|
private static let MAX_RESULTS_COUNT = 5
|
|
35
20
|
|
|
36
21
|
func reverseGeocode(latitude: Double, longitude: Double, call: CAPPluginCall) {
|
|
@@ -49,12 +34,7 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
49
34
|
if let error = error {
|
|
50
35
|
call.reject(error.message)
|
|
51
36
|
} else {
|
|
52
|
-
|
|
53
|
-
let result = try? JSONSerialization.jsonObject(with: encodedResult, options: .allowFragments) as? JSObject {
|
|
54
|
-
call.resolve(result)
|
|
55
|
-
} else {
|
|
56
|
-
call.reject("Invalid JSON result")
|
|
57
|
-
}
|
|
37
|
+
call.resolve(["addresses": resultObj])
|
|
58
38
|
}
|
|
59
39
|
})
|
|
60
40
|
}
|
|
@@ -93,32 +73,10 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
93
73
|
|
|
94
74
|
if let placemarks = placemarks {
|
|
95
75
|
let maxResultObjects = placemarks.count >= maxResults ? maxResults : placemarks.count
|
|
96
|
-
var resultObj = [
|
|
76
|
+
var resultObj = [JSObject]()
|
|
97
77
|
|
|
98
78
|
for i in 0..<maxResultObjects {
|
|
99
|
-
|
|
100
|
-
var latitude = ""
|
|
101
|
-
if let lat = placemarks[i].location?.coordinate.latitude {
|
|
102
|
-
latitude = "\(lat)"
|
|
103
|
-
}
|
|
104
|
-
var longitude = ""
|
|
105
|
-
if let lon = placemarks[i].location?.coordinate.longitude {
|
|
106
|
-
longitude = "\(lon)"
|
|
107
|
-
}
|
|
108
|
-
let placemark = NativeGeocoderResult(
|
|
109
|
-
latitude: latitude,
|
|
110
|
-
longitude: longitude,
|
|
111
|
-
countryCode: placemarks[i].isoCountryCode ?? "",
|
|
112
|
-
countryName: placemarks[i].country ?? "",
|
|
113
|
-
postalCode: placemarks[i].postalCode ?? "",
|
|
114
|
-
administrativeArea: placemarks[i].administrativeArea ?? "",
|
|
115
|
-
subAdministrativeArea: placemarks[i].subAdministrativeArea ?? "",
|
|
116
|
-
locality: placemarks[i].locality ?? "",
|
|
117
|
-
subLocality: placemarks[i].subLocality ?? "",
|
|
118
|
-
thoroughfare: placemarks[i].thoroughfare ?? "",
|
|
119
|
-
subThoroughfare: placemarks[i].subThoroughfare ?? "",
|
|
120
|
-
areasOfInterest: placemarks[i].areasOfInterest ?? []
|
|
121
|
-
)
|
|
79
|
+
let placemark = makePosition(placemarks[i])
|
|
122
80
|
resultObj.append(placemark)
|
|
123
81
|
}
|
|
124
82
|
|
|
@@ -146,12 +104,7 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
146
104
|
if let error = error {
|
|
147
105
|
call.reject(error.message)
|
|
148
106
|
} else {
|
|
149
|
-
|
|
150
|
-
let result = try? JSONSerialization.jsonObject(with: encodedResult, options: .allowFragments) as? JSObject {
|
|
151
|
-
call.resolve(result)
|
|
152
|
-
} else {
|
|
153
|
-
call.reject("Invalid JSON result")
|
|
154
|
-
}
|
|
107
|
+
call.resolve(["addresses": resultObj])
|
|
155
108
|
}
|
|
156
109
|
})
|
|
157
110
|
}
|
|
@@ -181,6 +134,23 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
181
134
|
})
|
|
182
135
|
}
|
|
183
136
|
}
|
|
137
|
+
|
|
138
|
+
private func makePosition(_ placemark: CLPlacemark) -> JSObject {
|
|
139
|
+
var ret = JSObject()
|
|
140
|
+
ret["latitude"] = placemark.location?.coordinate.latitude ?? ""
|
|
141
|
+
ret["longitude"] = placemark.location?.coordinate.longitude ?? ""
|
|
142
|
+
ret["countryCode"] = placemark.isoCountryCode ?? ""
|
|
143
|
+
ret["countryName"] = placemark.country ?? ""
|
|
144
|
+
ret["postalCode"] = placemark.postalCode ?? ""
|
|
145
|
+
ret["administrativeArea"] = placemark.administrativeArea ?? ""
|
|
146
|
+
ret["subAdministrativeArea"] = placemark.subAdministrativeArea ?? ""
|
|
147
|
+
ret["locality"] = placemark.locality ?? ""
|
|
148
|
+
ret["subLocality"] = placemark.subLocality ?? ""
|
|
149
|
+
ret["thoroughfare"] = placemark.thoroughfare ?? ""
|
|
150
|
+
ret["subThoroughfare"] = placemark.subThoroughfare ?? ""
|
|
151
|
+
ret["areasOfInterest"] = placemark.areasOfInterest ?? []
|
|
152
|
+
return ret
|
|
153
|
+
}
|
|
184
154
|
|
|
185
155
|
private func createForwardGeocodeResult(_ placemarks: [CLPlacemark]?, _ error: Error?, maxResults: Int, completionHandler: @escaping ForwardGeocodeCompletionHandler) {
|
|
186
156
|
guard error == nil else {
|
|
@@ -190,27 +160,12 @@ struct NativeGeocoderOptions: Decodable {
|
|
|
190
160
|
|
|
191
161
|
if let placemarks = placemarks {
|
|
192
162
|
let maxResultObjects = placemarks.count >= maxResults ? maxResults : placemarks.count
|
|
193
|
-
var resultObj = [
|
|
163
|
+
var resultObj = [JSObject]()
|
|
194
164
|
|
|
195
165
|
for i in 0..<maxResultObjects {
|
|
196
166
|
if let latitude = placemarks[i].location?.coordinate.latitude,
|
|
197
167
|
let longitude = placemarks[i].location?.coordinate.longitude {
|
|
198
|
-
|
|
199
|
-
// https://developer.apple.com/documentation/corelocation/clplacemark
|
|
200
|
-
let placemark = NativeGeocoderResult(
|
|
201
|
-
latitude: "\(latitude)",
|
|
202
|
-
longitude: "\(longitude)",
|
|
203
|
-
countryCode: placemarks[i].isoCountryCode ?? "",
|
|
204
|
-
countryName: placemarks[i].country ?? "",
|
|
205
|
-
postalCode: placemarks[i].postalCode ?? "",
|
|
206
|
-
administrativeArea: placemarks[i].administrativeArea ?? "",
|
|
207
|
-
subAdministrativeArea: placemarks[i].subAdministrativeArea ?? "",
|
|
208
|
-
locality: placemarks[i].locality ?? "",
|
|
209
|
-
subLocality: placemarks[i].subLocality ?? "",
|
|
210
|
-
thoroughfare: placemarks[i].thoroughfare ?? "",
|
|
211
|
-
subThoroughfare: placemarks[i].subThoroughfare ?? "",
|
|
212
|
-
areasOfInterest: placemarks[i].areasOfInterest ?? []
|
|
213
|
-
)
|
|
168
|
+
let placemark = makePosition(placemarks[i])
|
|
214
169
|
resultObj.append(placemark)
|
|
215
170
|
}
|
|
216
171
|
}
|