SWIFT – Change the map type (mixed, satellite) by segmentation control

I am trying to use the segment control button to change the map type, I hope it can use 3 options to change the map type: standard, satellite and hybrid. So far, I have this code but Once a different map type is selected, nothing happens:

@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
}
}

I am new to Swift and Xcode so I appreciate any help:)

Thanks

First, make sure to call the method when the segmented control selection changes. It’s easy to forget to connect to the socket method. Once you confirm this, remember that the map data is loaded asynchronously, so after choosing another mode, you may You can’t see it change immediately. However, using the code you posted, you will never see the .Hybrid type, because the selectedSegmentIndex in the 3-segment control will never be 3.

Implementation A more concise way of this method is:

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

Please note that Swift does not need a break statement at the end of each case.

Edit: Swift 4.1

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

Me I am trying to use the segment control button to change the map type, I hope it can use 3 options to change the map type: standard, satellite and hybrid. So far, I have this code but once a different map type is selected, nothing happens:

@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
}
}

I am new to Swift and Xcode so I appreciate any help:)

Thank you

First of all, make sure to The method is called when the segmented control selection changes. It is easy to forget the connection socket method. Once this is confirmed, remember that the map data is loaded asynchronously, so you may not see it change immediately after selecting another mode. However, Using the code you posted, you will never see the .Hybrid type, because the selectedSegmentIndex in the 3-segment control will never be 3.

The more concise way to implement this method is:< /p>

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

< p>Please note that Swift does not need a break statement at the end of each case.

Edit: Swift 4.1

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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4122 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.