Rakesh Mohan
Rakesh Mohan

Reputation: 609

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?

Upvotes: 2

Views: 5929

Answers (2)

Er.Shreyansh Shah
Er.Shreyansh Shah

Reputation: 1482

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.

Upvotes: 6

user4860907
user4860907

Reputation:

No need for a custom class. Just loop through the locations to create an annotation for each location. Assuming Locations hold CLLocations and cityNames hold city names and myMap is the map (UI).

var counter = 0
var allLocations = [MKPointAnnotation]()
for location in Locations {
    let loc = MKPointAnnotation()
    loc.title = cityNames[counter]
    loc.subtitle = String(counter + 1)
    loc.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude,
                                            longitude: location.coordinate.longitude);
    allLocations.append(loc)
    counter += 1 
}
// show annotations
myMap.addAnnotations(allLocations as [MKAnnotation])
myMap.showAnnotations(myMap.annotations, animated: true)

Upvotes: 0

Related Questions