Center Map on iOS in Swift

1

Hi, I'm doing a tourism application in which there are several cabins, places to eat and visit in this case, each one has a button that goes to a single map in which there are markers to show the location, and I need the center of the map load depending on the button that was pressed. I add the code of the mapController.

import UIKit
import MapKit

class mapController: UIViewController {


    @IBOutlet weak var myMapView: MKMapView!


    override func viewDidLoad() {
        super.viewDidLoad()
        mapTajMahal()

    }

    func mapTajMahal()
    {
        //Taj Mahal Coordinates: 27.175015, 78.042139

        // Coordinates
        let tajLat:CLLocationDegrees = -39.849254
        let tajLong:CLLocationDegrees = -71.994215

        let tajCoordinate = CLLocationCoordinate2D(latitude: tajLat, longitude: tajLong)

        let mocho = CLLocationCoordinate2D(latitude: -39.849152, longitude: -71.943082)
        let aitue = CLLocationCoordinate2D(latitude: -39.849581, longitude: -71.945078)
        let rucapillan = CLLocationCoordinate2D(latitude: -39.833403841808995, longitude:-72.07239013854979)
        let c_elportal = CLLocationCoordinate2D(latitude: -39.848834,longitude: -71.937480)
        let c_los_alamos = CLLocationCoordinate2D(latitude: -39.851652, longitude: -71.931052)
        let c_lago_neltume = CLLocationCoordinate2D(latitude: -39.77999, longitude: -71.95708)
        let c_peumayen = CLLocationCoordinate2D(latitude: -39.848467, longitude: -71.938013)
        let domos = CLLocationCoordinate2D(latitude: -39.850835, longitude: -71.940623)
        let patagonia = CLLocationCoordinate2D(latitude: -39.858635, longitude: -71.918520)
        let madero = CLLocationCoordinate2D(latitude: -39.858635, longitude: -71.918520
        )

        //Span
        let latDelta:CLLocationDegrees = 0.2
        let longDelta:CLLocationDegrees = 0.2
        let tajSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)

        let tajRegion = MKCoordinateRegion(center: tajCoordinate, span: tajSpan)

        myMapView.setRegion(tajRegion, animated: true )

        let tajAnnotation = MKPointAnnotation()
        tajAnnotation.title = "Neltume"
        tajAnnotation.subtitle = "She walks in beauty"
        tajAnnotation.coordinate = tajCoordinate


        let mochoannotation = MKPointAnnotation()
        mochoannotation.title = "Mocho Neltume"
        mochoannotation.coordinate = mocho

        let aitueanotation = MKPointAnnotation()
        aitueanotation.title = "Aitue Expediciones"
        aitueanotation.coordinate = aitue

        let rucapillananotation = MKPointAnnotation()
        rucapillananotation.title = "Rafting Excursiones Restaurant Hostal Cabañas Rucapillan"
        rucapillananotation.coordinate = rucapillan

        let c_elportalanotation = MKPointAnnotation()
        c_elportalanotation.title = "Cabañas el Portal"
        c_elportalanotation.coordinate = c_elportal

        let c_los_alamosanotation = MKPointAnnotation()
        c_los_alamosanotation.title = "Cabañas los Alamos"
        c_los_alamosanotation.coordinate = c_los_alamos

        let c_lago_neltumeanotation = MKPointAnnotation()
        c_lago_neltumeanotation.title = "Cabañas Lago Neltume"
        c_lago_neltumeanotation.coordinate = c_lago_neltume

        let c_peumayenatonation = MKPointAnnotation()
        c_peumayenatonation.title = "Cabañas Peumayen"
        c_peumayenatonation.coordinate = c_peumayen

        let domosanotation = MKPointAnnotation()
        domosanotation.title = "Cabañas Karü Domos del Fuy"
        domosanotation.coordinate = domos

        let patagoniaanotation = MKPointAnnotation()
        patagoniaanotation.title = "Cabañas Patagonia Mawida"
        patagoniaanotation.coordinate = patagonia

        let maderoanotation = MKPointAnnotation()
        maderoanotation.title = "Restaurante Refugio del Madero"
        maderoanotation.coordinate = madero

        myMapView.addAnnotation(mochoannotation)
        myMapView.addAnnotation(aitueanotation)
        myMapView.addAnnotation(rucapillananotation)
        myMapView.addAnnotation(c_elportalanotation)
        myMapView.addAnnotation(c_los_alamosanotation)
        myMapView.addAnnotation(c_lago_neltumeanotation)
        myMapView.addAnnotation(c_peumayenatonation)
        myMapView.addAnnotation(patagoniaanotation)
        myMapView.addAnnotation(maderoanotation)

    }

}
    
asked by Nicolas Rudisky 22.08.2016 в 17:51
source

2 answers

0

In the code you already have the setRegion(_:) which is precisely for that but the problem may be that you are doing it in viewDidLoad . Try calling the function in viewDidAppear to give the map time to load. If it does not work, use the mapView delegate that lets you know when you have finished loading the map and then you center the map with setRegion(_:)

    
answered by 22.08.2016 в 19:52
0

You can simply add a function something like this:

func centerInPosition(position:CLLocationCoordinate2D){
    map.setCenterCoordinate(position, animated: true)
}

and the flame:

centerInPosition(tajAnnotation.coordinate)

You should have an arrangement or a dictionary with the MKPointAnnotation to know which one to go to.

    
answered by 12.09.2016 в 18:52