Skip to content

Commit 049d8c9

Browse files
committed
Codable rectangular region
1 parent e56631d commit 049d8c9

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

MapboxGeocoder.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
357B4358202CC90A00735521 /* reverse_address.json in Resources */ = {isa = PBXBuildFile; fileRef = 357B4357202CC90A00735521 /* reverse_address.json */; };
5050
357B4359202CC90A00735521 /* reverse_address.json in Resources */ = {isa = PBXBuildFile; fileRef = 357B4357202CC90A00735521 /* reverse_address.json */; };
5151
357B435A202CC90A00735521 /* reverse_address.json in Resources */ = {isa = PBXBuildFile; fileRef = 357B4357202CC90A00735521 /* reverse_address.json */; };
52+
35D3DE392112410A00B62912 /* CoreLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35D3DE382112410A00B62912 /* CoreLocation.swift */; };
53+
35D3DE3A2112410A00B62912 /* CoreLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35D3DE382112410A00B62912 /* CoreLocation.swift */; };
54+
35D3DE3B2112410A00B62912 /* CoreLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35D3DE382112410A00B62912 /* CoreLocation.swift */; };
55+
35D3DE3C2112410A00B62912 /* CoreLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35D3DE382112410A00B62912 /* CoreLocation.swift */; };
5256
DA1AC0221E5C23B8006DF1D6 /* Contacts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA1AC0211E5C23B8006DF1D6 /* Contacts.framework */; };
5357
DA1AC0241E5C2425006DF1D6 /* Contacts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA1AC0231E5C2425006DF1D6 /* Contacts.framework */; };
5458
DA1AC0261E5C2436006DF1D6 /* Contacts.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA1AC0251E5C2436006DF1D6 /* Contacts.framework */; };
@@ -220,6 +224,7 @@
220224
07CF85C620F6DFEB007B26B6 /* permanent_reverse_single_valid.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = permanent_reverse_single_valid.json; sourceTree = "<group>"; };
221225
35506B8A200F856400629509 /* BridgingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BridgingTests.m; sourceTree = "<group>"; };
222226
357B4357202CC90A00735521 /* reverse_address.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = reverse_address.json; sourceTree = "<group>"; };
227+
35D3DE382112410A00B62912 /* CoreLocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreLocation.swift; sourceTree = "<group>"; };
223228
DA1AC0211E5C23B8006DF1D6 /* Contacts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Contacts.framework; path = System/Library/Frameworks/Contacts.framework; sourceTree = SDKROOT; };
224229
DA1AC0231E5C2425006DF1D6 /* Contacts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Contacts.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Contacts.framework; sourceTree = DEVELOPER_DIR; };
225230
DA1AC0251E5C2436006DF1D6 /* Contacts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Contacts.framework; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS3.1.sdk/System/Library/Frameworks/Contacts.framework; sourceTree = DEVELOPER_DIR; };
@@ -414,6 +419,7 @@
414419
DA2EC05B1CED72E900D4BA5D /* MBPlacemarkScope.swift */,
415420
DA2E03F11CB0FE0200D1269A /* MBRectangularRegion.swift */,
416421
DDC2470619A1C3B40054B0C0 /* Supporting Files */,
422+
35D3DE382112410A00B62912 /* CoreLocation.swift */,
417423
);
418424
path = MapboxGeocoder;
419425
sourceTree = "<group>";
@@ -967,6 +973,7 @@
967973
DA5170AF1CF1B1E000CD6DCF /* MBGeocoder.swift in Sources */,
968974
DA5170B21CF1B1E000CD6DCF /* MBPlacemarkScope.swift in Sources */,
969975
DA5170AE1CF1B1E000CD6DCF /* MBGeocodeOptions.swift in Sources */,
976+
35D3DE3A2112410A00B62912 /* CoreLocation.swift in Sources */,
970977
);
971978
runOnlyForDeploymentPostprocessing = 0;
972979
};
@@ -992,6 +999,7 @@
992999
DA5170DA1CF2541C00CD6DCF /* MBGeocoder.swift in Sources */,
9931000
DA5170DD1CF2541C00CD6DCF /* MBPlacemarkScope.swift in Sources */,
9941001
DA5170D91CF2541C00CD6DCF /* MBGeocodeOptions.swift in Sources */,
1002+
35D3DE3B2112410A00B62912 /* CoreLocation.swift in Sources */,
9951003
);
9961004
runOnlyForDeploymentPostprocessing = 0;
9971005
};
@@ -1017,6 +1025,7 @@
10171025
DA5170F51CF2582F00CD6DCF /* MBGeocoder.swift in Sources */,
10181026
DA5170F81CF2582F00CD6DCF /* MBPlacemarkScope.swift in Sources */,
10191027
DA5170F41CF2582F00CD6DCF /* MBGeocodeOptions.swift in Sources */,
1028+
35D3DE3C2112410A00B62912 /* CoreLocation.swift in Sources */,
10201029
);
10211030
runOnlyForDeploymentPostprocessing = 0;
10221031
};
@@ -1038,6 +1047,7 @@
10381047
DDC2295E1A360843006BE405 /* MBGeocoder.swift in Sources */,
10391048
DA2EC05C1CED72E900D4BA5D /* MBPlacemarkScope.swift in Sources */,
10401049
DA2E03F01CB0FDB400D1269A /* MBPlacemark.swift in Sources */,
1050+
35D3DE392112410A00B62912 /* CoreLocation.swift in Sources */,
10411051
);
10421052
runOnlyForDeploymentPostprocessing = 0;
10431053
};

MapboxGeocoder/CoreLocation.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import CoreLocation
2+
3+
4+
/// Codable CLLocationCoordinate2D conforming to the GeoJSON standard rfc7946 ([longitude, latitude])
5+
extension CLLocationCoordinate2D: Codable {
6+
public func encode(to encoder: Encoder) throws {
7+
var container = encoder.unkeyedContainer()
8+
try container.encode(longitude)
9+
try container.encode(latitude)
10+
}
11+
12+
public init(from decoder: Decoder) throws {
13+
var container = try decoder.unkeyedContainer()
14+
let longitude = try container.decode(CLLocationDegrees.self)
15+
let latitude = try container.decode(CLLocationDegrees.self)
16+
self.init(latitude: latitude, longitude: longitude)
17+
}
18+
}

MapboxGeocoder/MBRectangularRegion.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
The `RectangularRegion` class defines a rectangular bounding box for a geographic region.
33
*/
44
@objc(MBRectangularRegion)
5-
open class RectangularRegion: CLRegion {
5+
open class RectangularRegion: CLRegion, Codable {
66
/** Coordinate at the southwest corner. */
77
@objc open var southWest: CLLocationCoordinate2D = CLLocationCoordinate2D()
88
/** Coordinate at the northeast corner. */
99
@objc open var northEast: CLLocationCoordinate2D = CLLocationCoordinate2D()
1010

11+
private enum CodingKeys: String, CodingKey {
12+
case southWest
13+
case northEast
14+
}
15+
1116
/**
1217
Creates a rectangular region with the given southwest and northeast corners.
1318

@@ -34,6 +39,19 @@ open class RectangularRegion: CLRegion {
3439
coder.encodeValue(ofObjCType: "{dd}", at: &northEast)
3540
}
3641

42+
public required init(from decoder: Decoder) throws {
43+
let container = try decoder.container(keyedBy: CodingKeys.self)
44+
self.southWest = try container.decode(CLLocationCoordinate2D.self, forKey: .southWest)
45+
self.northEast = try container.decode(CLLocationCoordinate2D.self, forKey: .northEast)
46+
super.init()
47+
}
48+
49+
public func encode(to encoder: Encoder) throws {
50+
var container = encoder.container(keyedBy: CodingKeys.self)
51+
try container.encode(southWest, forKey: .southWest)
52+
try container.encode(northEast, forKey: .northEast)
53+
}
54+
3755
@objc open override var hashValue: Int {
3856
return (southWest.latitude.hashValue + southWest.longitude.hashValue
3957
+ northEast.latitude.hashValue + northEast.longitude.hashValue)

MapboxGeocoderTests/ReverseGeocodingTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class ReverseGeocodingTests: XCTestCase {
7373
XCTAssertEqual(region.southWest.longitude, southWest.longitude, accuracy: 0.000000000001)
7474
XCTAssertEqual(region.northEast.latitude, northEast.latitude, accuracy: 0.000000000001)
7575
XCTAssertEqual(region.northEast.longitude, northEast.longitude, accuracy: 0.000000000001)
76+
77+
let encodedRegion = try! JSONEncoder().encode(region)
78+
let decodedRegion = try! JSONDecoder().decode(RectangularRegion.self, from: encodedRegion)
79+
80+
XCTAssertEqual(region, decodedRegion)
7681
}
7782

7883
func testInvalidReverseGeocode() {

0 commit comments

Comments
 (0)