【问题标题】:Google maps change mapType on segmented control click谷歌地图在分段控制点击时更改 mapType
【发布时间】:2018-01-28 08:18:31
【问题描述】:

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

在控制器加载时,它会显示地图、圆圈和标记。现在,当我单击分段控件以更改地图类型时,它会在选择时进入代码块,但从不更新地图类型。

请在此处查看代码。

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

        }
    }

【问题讨论】:

    标签: ios swift google-maps uisegmentedcontrol


    【解决方案1】:

    老实说,我不使用谷歌地图

    但是,要更改我使用的 Apple 地图中的 MapTypes

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

    也许它会起作用

    【讨论】:

    • 我认为这与我在上面的函数中的作用相同。不过还是谢谢。
    • 为什么不用苹果地图?
    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 2012-10-18
    • 2015-02-16
    • 2020-02-02
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    相关资源
    最近更新 更多