Rakesh Mohan Rakesh Mohan - 1 month ago 6
Swift Question

How can I add two annotations in mapview using swift ios?

let latitude:CLLocationDegrees = 76.0100
let longitude:CLLocationDegrees =25.3620


let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

let latDelta:CLLocationDegrees = 1
let lonDelta:CLLocationDegrees = 1
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

mapView.setRegion(region, animated: true)

let annotation:MKPointAnnotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Office ."
annotation.subtitle = "My Office"

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


print("Locations = \(locations)")

let userLocation: CLLocation = locations[0] as CLLocation

//var latitude:CLLocationDegrees = userLocation.coordinate
//var longitude:CLLocationDegrees = -121.934048
//var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

let latDelta:CLLocationDegrees = 0.01
let lonDelta:CLLocationDegrees = 0.01
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

let region:MKCoordinateRegion = MKCoordinateRegionMake(userLocation.coordinate, span)

mapView.setRegion(region, animated: true)


I have created one annotation for one location. Now I need to create one more annotation for another location and show it in the map. How can I show that?

Answer Source

Create a custom class as "MyAnnotation"

import UIKit
import MapKit

class MyAnnotation: NSObject,MKAnnotation {

    var title : String?
var subTit : String?
var coordinate : CLLocationCoordinate2D

init(title:String,coordinate : CLLocationCoordinate2D,subtitle:String){

    self.title = title;
    self.coordinate = coordinate;
    self.subTit = subtitle;

}

}

add below code in your viewdidload of viewcontroller :-

let latitude:CLLocationDegrees = 76.0100
        let longitude:CLLocationDegrees = 25.3620

        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

        //Second Location lat and long
        let latitudeSec:CLLocationDegrees = 75.0100
        let longitudeSec:CLLocationDegrees = 24.3620

        let locationSec:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitudeSec, longitudeSec)

        let span:MKCoordinateSpan = MKCoordinateSpanMake(1, 1)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

        mapView.setRegion(region, animated: true)


        let myAn1 = MyAnnotation(title: "Office", coordinate: location, subtitle: "MyOffice");

        let myAn2 = MyAnnotation(title: "Office 1", coordinate: locationSec, subtitle: "MyOffice 1");

        mapView.addAnnotation(myAn1);
        mapView.addAnnotation(myAn2);
        //Instead of writing two lines of annotation we can use addAnnotations() to add.

Instead of this you can do with for loop also.

Output of code

Hope this will help you.