【问题标题】:Change map type (hybrid, satellite) via segmented control通过分段控制更改地图类型(混合、卫星)
【发布时间】:2015-10-18 16:13:45
【问题描述】:

我正在尝试使用分段控制按钮更改地图类型,我希望它使用 3 个选项更改地图类型:标准、卫星和混合。到目前为止,我有这段代码,但是一旦选择了不同的地图类型,什么都不会发生:

@IBAction func segmentedControlAction(sender: UISegmentedControl!) {

    if sender.selectedSegmentIndex == 0{

        mapView.mapType = MKMapType.Standard
    }
    else if sender.selectedSegmentIndex == 1{

        mapView.mapType = MKMapType.Satellite
    }
    else if sender.selectedSegmentIndex == 3{

        mapView.mapType = MKMapType.Hybrid
    }
}

我是 Swift 和 Xcode 的新手,因此感谢任何帮助 :)

谢谢

【问题讨论】:

  • 最有可能 .. mapView 为 nil 或未调用该方法

标签: swift mkmapview hybrid


【解决方案1】:

首先,确保在分段控件选择更改时调用您的方法。很容易忘记连接插座方法。一旦您验证了这一点,请记住地图数据是异步加载的,因此您可能不会在选择其他模式后立即看到它发生变化。但是,使用您发布的代码,您将永远看到 .Hybrid 类型,因为 3 段控件中的 selectedSegmentIndex永远不会是 3。 p>

实现此方法的更简洁的方式是:

@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
    switch (sender.selectedSegmentIndex) {
        case 0:
            mapView.mapType = .Standard
        case 1:
            mapView.mapType = .Satellite
        default:
            mapView.mapType = .Hybrid
    }
}

请注意,Swift 不需要在每个 case 的末尾加上 break 语句。

编辑:Swift 4.1

@IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            mapView.mapType = .standard
        case 1:
            mapView.mapType = .satellite
        default:
            mapView.mapType = .hybrid
        }
    }

【讨论】:

  • 不是.normal,是:mapView.mapType = .standard
【解决方案2】:
import CoreLocation
import MapKit

class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    @IBOutlet weak var my_map: MKMapView!

    var my_location = CLLocationManager()

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

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        self.my_map.showsUserLocation = true
        self.my_map.delegate = self    
        self.my_location = CLLocationManager.init()
        self.my_location.delegate = self
        self.my_location.requestWhenInUseAuthorization()
        self.my_location.stopUpdatingLocation()            

        let location = CLLocationCoordinate2DMake(11.004556, 76.961632)
        let span = MKCoordinateSpanMake(0.1, 0.1)
        let region = MKCoordinateRegionMake(location, span)

        my_map.setRegion(region, animated: true)

        let annotation = MKPointAnnotation()

        annotation.coordinate = location
        annotation.title = "coimbatore"
        annotation.subtitle = "manchester city"

        my_map.addAnnotation(annotation)    
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {    
        /*let annotationview = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        annotationview.image = UIImage(named:"nature.jpeg")

        //let transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
        //annotationview.transform = transform

        return annotationview*/

        let annotationReuseId = "Place"

        var anView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationReuseId)

        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationReuseId)
        } else {
            anView?.annotation = annotation
        }

        anView?.image = UIImage(named: "annotation")

        let transform = CGAffineTransform(scaleX: 0.1, y: 0.1)

        anView?.transform = transform
        anView?.backgroundColor = UIColor.clear
        anView?.canShowCallout = false

        return anView
    }

    @IBAction func maptypes(_ sender: Any)
    {
        switch(sender as AnyObject).selectedSegmentIndex
        {
        case maptype.standardmap.rawValue:
            my_map.mapType = .standard
        case maptype.satellitemap.rawValue:
            my_map.mapType = .satellite
        case maptype.hybridmap.rawValue:
            my_map.mapType = .hybrid
        default:
            break
        }
    }

【讨论】:

猜你喜欢
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
相关资源
最近更新 更多