Fascinated by MapKit, can some one guide on zoom


I loved the material available here and in articles section at RW, i tried something very beginner level and was able to achieve some success with it , but what i need is to be able to zoom in on location if i keep pressing the zoom button, like when we pinch , below is my code section wise

Start of file ,

 import UIKit

import MapKit

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate


var mapView: MKMapView!

override func loadView() {

mapView = MKMapView()

view = mapView

Then i add a button to zoom in on the location

 let zoomButton = UIButton()
 zoomButton.setTitle("ZOOM", for: .normal)
 zoomButton.setTitleColor(.blue, for: .normal)
 zoomButton.frame = CGRect(x: 0, y: 0, width: 100, height: 50)

Then i add the segmentedView

 let segmentedView = UISegmentedControl(items: ["standard", "hybrid","satellite"])
 segmentedView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
  segmentedView.selectedSegmentIndex = 0

Then i set the default constraint to false and add zoom button and segmentedview

  segmentedView.translatesAutoresizingMaskIntoConstraints = false
        zoomButton.translatesAutoresizingMaskIntoConstraints = false

Then i add the constraints to both button and segmentedview

  let topViewConstraint =
            segmentedView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,
        constant: 8)
        let margin = view.layoutMarginsGuide
        let leadingConstraint = segmentedView.leadingAnchor.constraint(equalTo: margin.leadingAnchor)
        let trailingConstraint = segmentedView.trailingAnchor.constraint(equalTo: margin.trailingAnchor)
        let topZoom = zoomButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 35)
        let leadingZoom = zoomButton.leadingAnchor.constraint(equalTo: margin.leadingAnchor)
        let trailingZoom = zoomButton.trailingAnchor.constraint(equalTo: margin.trailingAnchor)
        topViewConstraint.isActive = true
        leadingConstraint.isActive = true
        trailingConstraint.isActive = true
        topZoom.isActive = true
        leadingZoom.isActive = true
        trailingZoom.isActive = true

Then i add target to segmentedView

segmentedView.addTarget( **self** , action: **#selector** (updateViewPatch), for: .valueChanged)

Then i add target to zoombutton

zoomButton.addTarget( **self** , action: **#selector** (MapViewController.pressedAction(sender:)), for: .touchUpInside)

Now i come out side the loadView and add the func for button and segmentedView

Out side of viewload method now, for segmentedView

 @objc func updateViewPatch(_ sender: UISegmentedControl)
        switch sender.selectedSegmentIndex
        case 0:
            mapView.mapType = .standard
        case 1:
            mapView.mapType = .hybrid
        case 2:
            mapView.mapType = .satellite

Now before i add the func for zoom button inside of viewdidload method

 manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest

Also add this line out side of viewdidload

let manager = CLLocationManager()

Now the function for button pressed

 @objc func pressedAction(sender: UIButton!)
      let locationManager = CLLocationManager()
        let location = manager.location

        let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)

        let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake((location?.coordinate.latitude)!, (location?.coordinate.longitude)!)

        var region: MKCoordinateRegion = MKCoordinateRegion(center: myLocation, span: span)

        mapView.setRegion(region, animated: true)
        self.mapView.showsUserLocation = true

Now with all this i am able to see a location on my real iphone device , but when i press the zoom button again and again it does not zoom in like when we pinch and stretch the map , how can i keep zooming in …

i tried adding

region.span.longitudeDelta = region.span.longitudeDelta + 0.01
        region.span.latitudeDelta = region.span.latitudeDelta + 0.01

By converting the let to var , but it remains zoomed at predefined coordinates and do not get closer to location , can any one please guide how i can zoom in by press of a button


@amitsrivastava Do you still have issues with this?

@shogunkaramazov - thanks, no you can kindly close the thread

This topic was automatically closed after 166 days. New replies are no longer allowed.