@capgo/nativegeocoder 0.1.5 → 0.1.8

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.
@@ -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 = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
33
- typealias ForwardGeocodeCompletionHandler = ([NativeGeocoderResult]?, NativeGeocoderError?) -> Void
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) {
@@ -45,16 +30,11 @@ struct NativeGeocoderOptions: Decodable {
45
30
  options.defaultLocale = call.getString("defaultLocale")
46
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(["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 = [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
 
@@ -142,16 +100,11 @@ struct NativeGeocoderOptions: Decodable {
142
100
  options.defaultLocale = call.getString("defaultLocale")
143
101
  options.maxResults = call.getInt("maxResults") ?? 1
144
102
 
145
- forwardGeocodeHandler(address, options: options, completionHandler: { [weak self] (resultObj, error) in
103
+ forwardGeocodeHandler(address, options: options, completionHandler: { (resultObj, error) in
146
104
  if let error = error {
147
105
  call.reject(error.message)
148
106
  } else {
149
- if let encodedResult = try? JSONEncoder().encode(resultObj),
150
- let result = try? JSONSerialization.jsonObject(with: encodedResult, options: .allowFragments) as? [Dictionary<String,Any>] {
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 = [NativeGeocoderResult]()
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/nativegeocoder",
3
- "version": "0.1.5",
3
+ "version": "0.1.8",
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",