Monday, June 1, 2015

SWIFT HOW TO CONVERT ADDRESS TO COORDINATE IN MKMapview


Today I am trying to play around with MKMapview, I want to display location once the program is loaded,

a) Using coordinate (Step 1 to Step 7)
b) Using address I key in myself (Step 1 to Step 8)



Step1: Open Xcode

Step2: In Main Storyboard, drag MKMapview into your ViewController

Step3: Create a new file Cocoa Touch Class


Step4: Name it MapVC, Subclass of UIViewController


Step5: On the storyboard, click your MKMapView and on the right hand side -> "identity inspector" change the Class to "MapVC"



Step6: On the storyboard, click "Assistant Editor" on top to open the code side by side with your MKMapView, Control Drag your MKMapView to your "MapVC" code to create a variable connection,

see the diagram
@IBOutlet weak var mapView: MKMapView!




Step7: Put in the below code so once you open the app, it will load the location base on the coordinate i put

////////////////////////////////////////////////////////////////////////////

import UIKit
import MapKit
import CoreLocation

class MapVC: UIViewController
{
    @IBOutlet weak var mapView: MKMapView!
    var  initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)

     

    override func viewDidLoad() {
        super.viewDidLoad()
        centerMapOnLocation(initialLocation)
    }


    func centerMapOnLocation(location: CLLocation)
    {
        
        let regionRadius: CLLocationDistance = 1000
        
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }
}


/////////////////////////////////////////////////////////////////////

Code Explanation:
first you create a new variable "initialLocation" then you hardcode it with the coordinate you like

then you create a function "centerMapOnLocation" that set the mapView region to 1000 meter within the coordinate you give

finally on viewDidLoad() you call this function providing the parameter "initialLocation"



Step8: If you want to use the address instead, then you need to convert the address into coordinate, to do this, modify the MapVC.swift to below
////////////////////////////////////////////////////////////////////////////////////////

import UIKit
import MapKit
import CoreLocation

class MapVC: UIViewController
{
    @IBOutlet weak var mapView: MKMapView!
    var  initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)

     

    override func viewDidLoad() {
        super.viewDidLoad()
       var address = "1 Infinite Loop, CA, USA"
        var geocoder = CLGeocoder()
        geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
            if let placemark = placemarks?[0] as? CLPlacemark
            {
               
                self.initialLocation = placemark.location
                self.centerMapOnLocation(self.initialLocation)
               
            }
        })
    }


    func centerMapOnLocation(location: CLLocation)
    {
        
        let regionRadius: CLLocationDistance = 1000
        
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }
}


/////////////////////////////////////////////////////////////////////

Code Explanation, so here you create a new variable "address" and type in the address that you like, then you use the CLGeocoder to convert the address into coordinate, load the coordinate into "initialLocation" then finally call the function "centerMapOnLocation" to display it in the map


There you go, hopefully it can help you in some way

No comments:

Post a Comment