Google地图会在分段控制点击上更改mapType

时间:2018-01-27 22:02:28

标签: ios swift google-maps uisegmentedcontrol

这是问题所在。在我的视图控制器中,我在顶部放置了一个分段控件。

在控制器加载时,它会显示地图,圆圈和标记。现在,当我单击分段控件来更改地图类型时,它会进入select的代码块,但永远不会更新Map类型。

请在此处查看代码。

class MapController: UIViewController, CLLocationManagerDelegate , GMSMapViewDelegate {

        var locationManager:CLLocationManager!
        let marker = GMSMarker()
        var roundCircle: GMSCircle!
        var mapView = GMSMapView()
        private var didPerformGeocode = false

        enum maptype:NSInteger
        {
            case standardmap = 0
            case satellitemap = 1
        }

        override func viewDidLoad() {
            super.viewDidLoad()
        }

        @IBAction func segmentedControlAction(_ sender: UISegmentedControl)         

         {
            switch sender.selectedSegmentIndex {

                case maptype.standardmap.rawValue:
                mapView.mapType = .normal

                **no change in the map type** 

                case maptype.satellitemap.rawValue:
                mapView.mapType = .satellite

                **no change in the map type** 

                default:
                break
            }
        }

        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            determineMyCurrentLocation()
        }


        func determineMyCurrentLocation() {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()

            if CLLocationManager.locationServicesEnabled() {
                locationManager.startUpdatingLocation()
            }

        }


        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let userLocation:CLLocation = locations[0] as CLLocation

            guard !didPerformGeocode else { return }
            didPerformGeocode = true
            locationManager.stopUpdatingLocation()

            let camera = GMSCameraPosition.camera(withLatitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude, zoom: 13.0)
            let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
            mapView.delegate = self
            self.view = mapView

            let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)
            let markerView = UIImageView(image: markerImage)
            markerView.tintColor = UIColor.red
            marker.iconView = markerView
            marker.position = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
            marker.map = mapView


            roundCircle = GMSCircle(position: camera.target, radius: 1500) //0.96 Miles
            roundCircle.fillColor = UIColor.red.withAlphaComponent(0.3)
            roundCircle.strokeColor = nil
            roundCircle.map = mapView

        }
    }

1 个答案:

答案 0 :(得分:0)

老实说,我没有使用谷歌地图

但是,要在Apple Maps中更改MapTypes,我使用

@IBAction func Whatever(_ sender: UISegmentedControl) {
   mapView.mapType = MKMapType.init(rawValue: UInt(sender.selectedSegmentIndex)) ?? .normal
}

也许它会起作用

相关问题