Using IBAction Buttons to Zoom MapView

前端 未结 6 1588
谎友^
谎友^ 2021-01-02 17:17

I have an issue. My current location is displayed and centered in a map view however the map region doesn\'t get zoomed in to. I tried taking Rob\'s advice by taking span an

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 18:10

    Swift 2.0:

    Using Extension :

    extension MKMapView{
    
     func zoomInPinAnnotationLocation(targetMapViewName : MKMapView?, delta: Double)
     {
      var region: MKCoordinateRegion = targetMapViewName!.region
      region.span.latitudeDelta /= delta
      region.span.longitudeDelta /= delta
      targetMapViewName!.region = region
    
     }
     func zoomOutPinAnnotationLocation(targetMapViewName : MKMapView?,delta: Double)
     {
      var region: MKCoordinateRegion = targetMapViewName!.region
      region.span.latitudeDelta *= delta
      region.span.longitudeDelta *= delta
      targetMapViewName!.region = region
     }
    
    }
    

    Usage:

    var mapViewZoomStepperValue: Double = -1.0
    @IBOutlet weak var mapViewZoomStepper: UIStepper!
    
    @IBAction func mapViewZoomStepperValueChanged(sender: AnyObject) {
    
      if (mapViewZoomStepper.value  > mapViewZoomStepperValue)
      {
       mapViewZoomStepperValue = mapViewZoomStepperValue + 1.0
    
    //Zoom In
       detailMapView.zoomInPinAnnotationLocation(detailMapView, delta: 3.0)
      }
      else
      {
       mapViewZoomStepperValue = mapViewZoomStepper.value - 1.0
    
    //Zoom Out
       detailMapView.zoomOutPinAnnotationLocation(detailMapView, delta: 3.0)
      }
    
     }
    

提交回复
热议问题