@capgo/nativegeocoder 0.1.4 → 0.1.7

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,21 +1,7 @@
1
1
  import Foundation
2
+ import Capacitor
2
3
  import CoreLocation
3
4
 
4
- struct NativeGeocoderResult: Encodable {
5
- var latitude: String?
6
- var longitude: String?
7
- var countryCode: String?
8
- var countryName: String?
9
- var postalCode: String?
10
- var administrativeArea: String?
11
- var subAdministrativeArea: String?
12
- var locality: String?
13
- var subLocality: String?
14
- var thoroughfare: String?
15
- var subThoroughfare: String?
16
- var areasOfInterest: [String]?
17
- }
18
-
19
5
  struct NativeGeocoderError {
20
6
  var message: String
21
7
  }
@@ -28,12 +14,11 @@ struct NativeGeocoderOptions: Decodable {
28
14
 
29
15
  @objc public class NativeGeocoder: NSObject {
30
16
 
31
- typealias ReverseGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
32
- typealias ForwardGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
17
+ typealias ReverseGeocodeCompletionHandler = ([JSObject]?, NativeGeocoderError?) -> Void
18
+ typealias ForwardGeocodeCompletionHandler = ([JSObject]?, NativeGeocoderError?) -> Void
33
19
  private static let MAX_RESULTS_COUNT = 5
34
20
 
35
- // MARK: - REVERSE GEOCODE
36
- @objc(reverseGeocode:) func reverseGeocode(latitude: Double, longitude: Double, call: CAPPluginCall) {
21
+ func reverseGeocode(latitude: Double, longitude: Double, call: CAPPluginCall) {
37
22
  if (CLGeocoder().isGeocoding) {
38
23
  call.reject("Geocoder is busy. Please try again later.")
39
24
  return
@@ -41,20 +26,15 @@ struct NativeGeocoderOptions: Decodable {
41
26
 
42
27
  let location = CLLocation(latitude: latitude, longitude: longitude)
43
28
  var options = NativeGeocoderOptions(useLocale: true, defaultLocale: nil, maxResults: 1)
44
- options.useLocale = call.getBoolean("useLocale") ?? true
45
- options.defaultLocale = call.getString()
46
- options.maxResults = call.getNumber() ?? 1
29
+ options.useLocale = call.getBool("useLocale") ?? true
30
+ options.defaultLocale = call.getString("defaultLocale")
31
+ options.maxResults = call.getInt("maxResults") ?? 1
47
32
 
48
- reverseGeocodeLocationHandler(location, options: options, completionHandler: { [weak self] (resultObj, error) in
33
+ reverseGeocodeLocationHandler(location, options: options, completionHandler: { (resultObj, error) in
49
34
  if let error = error {
50
35
  call.reject(error.message)
51
36
  } else {
52
- if let encodedResult = try? JSONEncoder().encode(resultObj),
53
- let result = try? JSONSerialization.jsonObject(with: encodedResult, options: .allowFragments) as? [Dictionary<String,Any>] {
54
- call.resolve(result)
55
- } else {
56
- call.reject("Invalid JSON result")
57
- }
37
+ call.resolve(["results": 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 = [NativeGeocoderResult]()
76
+ var resultObj = [JSObject]()
97
77
 
98
78
  for i in 0..<maxResultObjects {
99
- // https://developer.apple.com/documentation/corelocation/clplacemark
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
 
@@ -130,8 +88,7 @@ struct NativeGeocoderOptions: Decodable {
130
88
  }
131
89
 
132
90
 
133
- // MARK: - FORWARD GEOCODE
134
- @objc(forwardGeocode:)func forwardGeocode(address: String, call: CAPPluginCall) {
91
+ func forwardGeocode(address: String, call: CAPPluginCall) {
135
92
 
136
93
  if (CLGeocoder().isGeocoding) {
137
94
  call.reject("Geocoder is busy. Please try again later.")
@@ -139,20 +96,15 @@ struct NativeGeocoderOptions: Decodable {
139
96
  }
140
97
 
141
98
  var options = NativeGeocoderOptions(useLocale: true, defaultLocale: nil, maxResults: 1)
142
- options.useLocale = call.getBoolean("useLocale") ?? true
143
- options.defaultLocale = call.getString()
144
- options.maxResults = call.getNumber() ?? 1
99
+ options.useLocale = call.getBool("useLocale") ?? true
100
+ options.defaultLocale = call.getString("defaultLocale")
101
+ options.maxResults = call.getInt("maxResults") ?? 1
145
102
 
146
- forwardGeocodeHandler(address, options: options, completionHandler: { [weak self] (resultObj, error) in
103
+ forwardGeocodeHandler(address, options: options, completionHandler: { (resultObj, error) in
147
104
  if let error = error {
148
105
  call.reject(error.message)
149
106
  } else {
150
- if let encodedResult = try? JSONEncoder().encode(resultObj),
151
- let result = try? JSONSerialization.jsonObject(with: encodedResult, options: .allowFragments) as? [Dictionary<String,Any>] {
152
- call.resolve(result)
153
- } else {
154
- call.reject("Invalid JSON result")
155
- }
107
+ call.resolve(["results": resultObj])
156
108
  }
157
109
  })
158
110
  }
@@ -182,6 +134,23 @@ struct NativeGeocoderOptions: Decodable {
182
134
  })
183
135
  }
184
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
+ }
185
154
 
186
155
  private func createForwardGeocodeResult(_ placemarks: [CLPlacemark]?, _ error: Error?, maxResults: Int, completionHandler: @escaping ForwardGeocodeCompletionHandler) {
187
156
  guard error == nil else {
@@ -191,27 +160,12 @@ struct NativeGeocoderOptions: Decodable {
191
160
 
192
161
  if let placemarks = placemarks {
193
162
  let maxResultObjects = placemarks.count >= maxResults ? maxResults : placemarks.count
194
- var resultObj = [NativeGeocoderResult]()
163
+ var resultObj = [JSObject]()
195
164
 
196
165
  for i in 0..<maxResultObjects {
197
166
  if let latitude = placemarks[i].location?.coordinate.latitude,
198
167
  let longitude = placemarks[i].location?.coordinate.longitude {
199
-
200
- // https://developer.apple.com/documentation/corelocation/clplacemark
201
- let placemark = NativeGeocoderResult(
202
- latitude: "\(latitude)",
203
- longitude: "\(longitude)",
204
- countryCode: placemarks[i].isoCountryCode ?? "",
205
- countryName: placemarks[i].country ?? "",
206
- postalCode: placemarks[i].postalCode ?? "",
207
- administrativeArea: placemarks[i].administrativeArea ?? "",
208
- subAdministrativeArea: placemarks[i].subAdministrativeArea ?? "",
209
- locality: placemarks[i].locality ?? "",
210
- subLocality: placemarks[i].subLocality ?? "",
211
- thoroughfare: placemarks[i].thoroughfare ?? "",
212
- subThoroughfare: placemarks[i].subThoroughfare ?? "",
213
- areasOfInterest: placemarks[i].areasOfInterest ?? []
214
- )
168
+ let placemark = makePosition(placemarks[i])
215
169
  resultObj.append(placemark)
216
170
  }
217
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/nativegeocoder",
3
- "version": "0.1.4",
3
+ "version": "0.1.7",
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",