Skip to content

Commit 944d41f

Browse files
committed
define protocol MapScaleCalculator for integration with different MapView
1 parent 89f3926 commit 944d41f

File tree

8 files changed

+65
-23
lines changed

8 files changed

+65
-23
lines changed

MapScaleView_iOS_API.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
"@executable_path/Frameworks",
380380
);
381381
MARKETING_VERSION = 1.1.0;
382-
PRODUCT_BUNDLE_IDENTIFIER = "xattacker.MapScaleView-iOS-API";
382+
PRODUCT_BUNDLE_IDENTIFIER = com.xattacker.MapScaleSample;
383383
PRODUCT_NAME = "$(TARGET_NAME)";
384384
PROVISIONING_PROFILE_SPECIFIER = "";
385385
SWIFT_VERSION = 5.0;
@@ -401,7 +401,7 @@
401401
"@executable_path/Frameworks",
402402
);
403403
MARKETING_VERSION = 1.1.0;
404-
PRODUCT_BUNDLE_IDENTIFIER = "xattacker.MapScaleView-iOS-API";
404+
PRODUCT_BUNDLE_IDENTIFIER = com.xattacker.MapScaleSample;
405405
PRODUCT_NAME = "$(TARGET_NAME)";
406406
PROVISIONING_PROFILE_SPECIFIER = "";
407407
SWIFT_VERSION = 5.0;

MapScaleView_iOS_API.xcodeproj/xcuserdata/xattacker.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>MapScaleView_iOS_API.xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
10-
<integer>0</integer>
10+
<integer>1</integer>
1111
</dict>
1212
</dict>
1313
</dict>

MapScaleView_iOS_API/AppDelegate.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import UIKit
1010
import GoogleMaps
1111

1212
@UIApplicationMain
13-
class AppDelegate: UIResponder, UIApplicationDelegate {
14-
13+
class AppDelegate: UIResponder, UIApplicationDelegate
14+
{
1515
var window: UIWindow?
1616

17-
1817
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1918
// Override point for customization after application launch.
2019

MapScaleView_iOS_API/Base.lproj/Main.storyboard

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@
8181
<constraints>
8282
<constraint firstAttribute="height" constant="30" id="dpQ-Uc-KpQ"/>
8383
</constraints>
84+
<userDefinedRuntimeAttributes>
85+
<userDefinedRuntimeAttribute type="color" keyPath="outlineColor">
86+
<color key="value" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
87+
</userDefinedRuntimeAttribute>
88+
<userDefinedRuntimeAttribute type="color" keyPath="bodyColor">
89+
<color key="value" systemColor="systemIndigoColor"/>
90+
</userDefinedRuntimeAttribute>
91+
</userDefinedRuntimeAttributes>
8492
</view>
8593
</subviews>
8694
<viewLayoutGuide key="safeArea" id="PlR-dh-pct"/>
@@ -128,5 +136,8 @@
128136
<systemColor name="systemBackgroundColor">
129137
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
130138
</systemColor>
139+
<systemColor name="systemIndigoColor">
140+
<color red="0.34509803921568627" green="0.33725490196078434" blue="0.83921568627450982" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
141+
</systemColor>
131142
</resources>
132143
</document>

MapScaleView_iOS_API/GoogleMapViewController.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ import UIKit
1010
import GoogleMaps
1111

1212

13-
// ScaleView setup with
13+
// ScaleView setup with GMSMapView
1414
class GoogleMapViewController: UIViewController, GMSMapViewDelegate
1515
{
16-
@IBOutlet weak var mapView: GMSMapView!
17-
@IBOutlet weak var scaleView: UIMapScaleView!
16+
@IBOutlet private weak var mapView: GMSMapView!
17+
@IBOutlet private weak var scaleView: UIMapScaleView!
1818

1919
override func viewDidLoad()
2020
{
2121
super.viewDidLoad()
2222

2323
// Do any additional setup after loading the view.
24+
// set positon to the center of Taiwan
25+
let position = GMSCameraPosition(latitude: 23.80030512974474, longitude: 120.96817418343652, zoom: 8)
26+
self.mapView.camera = position
27+
28+
self.mapView.delegate = self
29+
self.scaleView.mapScaleCalculator = self.mapView
2430
}
2531

2632
// MARK: implement from GMSMapViewDelegate
@@ -29,3 +35,17 @@ class GoogleMapViewController: UIViewController, GMSMapViewDelegate
2935
self.scaleView.setNeedsLayout()
3036
}
3137
}
38+
39+
40+
// make GMSMapView could support ScaleView
41+
extension GMSMapView: MapScaleCalculator
42+
{
43+
public var metersPerPixel: CGFloat
44+
{
45+
let topLeft = self.projection.visibleRegion().farLeft
46+
let bottomLeft = self.projection.visibleRegion().nearLeft
47+
let lat = CGFloat(abs(Float(topLeft.latitude - bottomLeft.latitude)))
48+
let metersPerPixel = CGFloat((cos(lat * .pi / 180) * 2 * .pi) * 6378137 / CGFloat((256 * pow(2, self.camera.zoom))))
49+
return metersPerPixel
50+
}
51+
}

MapScaleView_iOS_API/Info.plist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
<key>UISupportedInterfaceOrientations</key>
3232
<array>
3333
<string>UIInterfaceOrientationPortrait</string>
34-
<string>UIInterfaceOrientationLandscapeLeft</string>
35-
<string>UIInterfaceOrientationLandscapeRight</string>
34+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
3635
</array>
3736
<key>UISupportedInterfaceOrientations~ipad</key>
3837
<array>

MapScaleView_iOS_API/Sources/UIMapScaleView.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ public enum MapScaleDistanceUnit
2323
case imperial // UK
2424
}
2525

26+
public protocol MapScaleCalculator: class
27+
{
28+
var metersPerPixel: CGFloat { get }
29+
}
30+
31+
32+
extension MKMapView: MapScaleCalculator
33+
{
34+
public var metersPerPixel: CGFloat
35+
{
36+
let horizontalDistance = MKMetersPerMapPointAtLatitude(self.centerCoordinate.latitude)
37+
let metersPerPixel = CGFloat(self.visibleMapRect.size.width * horizontalDistance) / self.bounds.size.width
38+
return metersPerPixel
39+
}
40+
}
41+
2642

2743
@IBDesignable
2844
public final class UIMapScaleView: UIView
@@ -71,7 +87,8 @@ public final class UIMapScaleView: UIView
7187
}
7288
}
7389

74-
private weak var mapView: MKMapView?
90+
public weak var mapScaleCalculator: MapScaleCalculator?
91+
7592
private weak var distanceLabel: UILabel?
7693
private var scaledWidth = CGFloat(0)
7794

@@ -93,16 +110,13 @@ public final class UIMapScaleView: UIView
93110
{
94111
super.layoutSubviews()
95112

96-
guard let mapView = self.mapView else
113+
guard let calculator = self.mapScaleCalculator else
97114
{
98115
return
99116
}
100117

101118

102-
let horizontalDistance = MKMetersPerMapPointAtLatitude(mapView.centerCoordinate.latitude)
103-
let metersPerPixel = CGFloat(mapView.visibleMapRect.size.width * horizontalDistance) / mapView.bounds.size.width
104-
let distance = self.calculateDistance(metersPerPixel)
105-
119+
let distance = self.calculateDistance(calculator.metersPerPixel)
106120
self.scaledWidth = distance.width
107121
self.setNeedsDisplay()
108122

@@ -203,9 +217,9 @@ public final class UIMapScaleView: UIView
203217
}
204218
}
205219

206-
public func setupMap(_ mapView: MKMapView)
220+
deinit
207221
{
208-
self.mapView = mapView
222+
self.mapScaleCalculator = nil
209223
}
210224
}
211225

MapScaleView_iOS_API/ViewController.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ import MapKit
1313
// ScaleView setup with MKMapView
1414
class ViewController: UIViewController, MKMapViewDelegate
1515
{
16-
@IBOutlet weak var mapView: MKMapView!
17-
@IBOutlet weak var scaleView: UIMapScaleView!
16+
@IBOutlet private weak var mapView: MKMapView!
17+
@IBOutlet private weak var scaleView: UIMapScaleView!
1818

1919
override func viewDidLoad()
2020
{
2121
super.viewDidLoad()
2222
// Do any additional setup after loading the view, typically from a nib.
2323

2424
self.mapView.delegate = self
25-
self.scaleView.setupMap(self.mapView)
25+
self.scaleView.mapScaleCalculator = self.mapView
2626
//self.scaleView.unit = .imperial
2727
}
2828

@@ -32,4 +32,3 @@ class ViewController: UIViewController, MKMapViewDelegate
3232
self.scaleView.setNeedsLayout()
3333
}
3434
}
35-

0 commit comments

Comments
 (0)