Wednesday 3 August 2016

MKMapView for required location using Latitude and Longitude - iOS Swift

This Code will be really useful to find the location of the required place. We need to give latitude and longitude as Input .

For example :
      Here I want to Find the location of S2 Theatre, Thiruvanmiyur, Chennai . So, First I want to collect the latitude and longitude of the location.
                                   Latitude    =  12.9895  Longitude = 80.2565
Step 1 :
               Insert a Mapview in Main storyboard and also Create an IBOutlet for MKMapView in View Controller.
Step 2 :
               Copy this code into the viewDidLoad. Inside the variable location you have to include the latitude and longitude. Inside the variable span you have to include the zooming size that which required. Annotation is nothing but the marker present in the location.

        let location = CLLocationCoordinate2DMake(12.9895, 80.2565) 
        let span = MKCoordinateSpanMake(0.0002, 0.0002)
        let region = MKCoordinateRegion(center: location,span: span)
        mapView.setRegion(region, animated: true)
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "S2 Theatre"

Output :

No comments: