# Fascinated by MapKit, can some one guide on zoom

**URL:** <https://forums.kodeco.com/t/fascinated-by-mapkit-can-some-one-guide-on-zoom/94988>\
**Category:** iOS Apprentice\
**Created:** [January 18, 2020, 9:19am UTC](https://forums.kodeco.com/t/fascinated-by-mapkit-can-some-one-guide-on-zoom/94988 "2020-01-18T09:19:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitsrivastava](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amitsrivastava/32/81494_2.png) [@amitsrivastava](https://forums.kodeco.com/u/amitsrivastava)\
**Post date:** [January 18, 2020, 9:19am UTC](https://forums.kodeco.com/t/fascinated-by-mapkit-can-some-one-guide-on-zoom/94988/1 "2020-01-18T09:19:42Z")

</div>

Hi,

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
        view.addSubview(segmentedView)
        view.addSubview(zoomButton)

```

**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**

zo`omButton.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
            
        default:
            break
        }
    }

```

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

```
 manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

```

**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()
      locationManager.requestWhenInUseAuthorization()
     
        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

Thanks

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [March 27, 2020, 7:21pm UTC](https://forums.kodeco.com/t/fascinated-by-mapkit-can-some-one-guide-on-zoom/94988/2 "2020-03-27T19:21:06Z")

</div>

@amitsrivastava Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![amitsrivastava](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/amitsrivastava/32/81494_2.png) [@amitsrivastava](https://forums.kodeco.com/u/amitsrivastava)\
**Post date:** [April 17, 2020, 1:08pm UTC](https://forums.kodeco.com/t/fascinated-by-mapkit-can-some-one-guide-on-zoom/94988/3 "2020-04-17T13:08:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [July 3, 2020, 1:19am UTC](https://forums.kodeco.com/t/fascinated-by-mapkit-can-some-one-guide-on-zoom/94988/4 "2020-07-03T01:19:45Z")

</div>

Closed on July 3, 2020, 1:19am UTC

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