@@ -10,17 +10,23 @@ import UIKit
1010import GoogleMaps
1111
1212
13- // ScaleView setup with
13+ // ScaleView setup with GMSMapView
1414class 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+ }
0 commit comments