2015-10-18 42 views
2

更改地圖類型(混合動力,衛星),我試圖改變使用分段控制按鈕的地圖類型,我想爲它帶有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 
    } 
} 

我是新來的斯威夫特和Xcode的,所以任何幫助表示讚賞:)

感謝

+0

最有可能.. MapView的是零或方法不叫 –

回答

20

首先,確保您的方法在分段控件選擇更改時被調用。忘記連接插座方法很容易。一旦你驗證,記住地圖數據異步加載,所以你可能看不到它選擇不同的模式後立即更改。然而,您發佈的代碼,你將永遠不會看到.Hybrid類型,因爲在3段控制selectedSegmentIndex永遠是3

一個實施該方法的更簡潔的方法是:

@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語句。

1
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 
     } 
    } 
+1

它通常是更好地解釋了一個解決方案,而不是僅僅張貼的匿名代碼的某些行。你可以閱讀[我怎樣寫一個很好的答案(https://stackoverflow.com/help/how-to-answer),並且還[解釋完全基於代碼的答案](https://meta.stackexchange.com /問題/ 114762 /解釋-entirely-%E2%80%8C%E2%80%8Bcode爲主,答案) –