Search code examples
xcodeswiftlocationmapkitios9

How do I zoom in on a location using mapkit in swift 2 (xcode)


I am trying to zoom into a point on the map when a user inputs an address, for example, one infinite loop. I want it to zoom in to the location to the street level but I cannot for the love of God get it to zoom in. I have tried messing with the region, the span, etc. but no luck. Here is what my code looks like (I have nothing in the view did load func)... you will see i have commented out some code because I tried it and nothing happened. my code and here is what the output is on the simulator the output on simulator


Solution

  • Why do you define func in geocodeAddressString closure?

    How about the following code?

        geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
            if (error != nil) {
                print("Error", error)
            }
    
            if let placemark = placemarks?.first {
                let span = MKCoordinateSpan(latitudeDelta: 0.0001, longitudeDelta: 0.0001)
                let coordinate = placemark.location!.coordinate
                let region = MKCoordinateRegion(center: coordinate, span: span)
                self.mapView.setRegion(region, animated: true)
            }
        })
    

    Result:

    enter image description here