Mohamed Salah
Mohamed Salah

Reputation: 1205

How to place annotations RANDOMLY in Mapkit Swift

I want to know how to pin annotations randomly in a MapView

    let latitude: CLLocationDegrees = 33.606800
    let longitude: CLLocationDegrees = -111.845360
    let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

    //map annotation
    let annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = "Taliesin West"
    annotation.subtitle = "Design"
    map.addAnnotation(annotation) 

Upvotes: 4

Views: 3201

Answers (2)

Mohamed Salah
Mohamed Salah

Reputation: 1205

For Anyone who wants the solution:

First: Adding Random Annotation according to current Location

Generating the Annotations:

func generateAnnoLoc() {

    var num = 0
    //First we declare While to repeat adding Annotation
    while num != 15 {
          num += 1

        //Add Annotation
        let annotation = MKPointAnnotation()
        annotation.coordinate = generateRandomCoordinates(70, max: 150) //this will be the maximum and minimum distance of the annotation from the current Location (Meters)
        annotation.title = "Annotation Title"
        annotation.subtitle = "SubTitle"
        mapView.addAnnotation(annotation)

    }

}

Generating Coordinates:

func generateRandomCoordinates(min: UInt32, max: UInt32)-> CLLocationCoordinate2D {
    //Get the Current Location's longitude and latitude
    let currentLong = currentLoc.coordinate.longitude
    let currentLat = currentLoc.coordinate.latitude

    //1 KiloMeter = 0.00900900900901° So, 1 Meter = 0.00900900900901 / 1000
    let meterCord = 0.00900900900901 / 1000

    //Generate random Meters between the maximum and minimum Meters
    let randomMeters = UInt(arc4random_uniform(max) + min)

    //then Generating Random numbers for different Methods 
    let randomPM = arc4random_uniform(6)

    //Then we convert the distance in meters to coordinates by Multiplying the number of meters with 1 Meter Coordinate
    let metersCordN = meterCord * Double(randomMeters)

    //here we generate the last Coordinates 
    if randomPM == 0 {
        return CLLocationCoordinate2D(latitude: currentLat + metersCordN, longitude: currentLong + metersCordN)
    }else if randomPM == 1 {
        return CLLocationCoordinate2D(latitude: currentLat - metersCordN, longitude: currentLong - metersCordN)
    }else if randomPM == 2 {
        return CLLocationCoordinate2D(latitude: currentLat + metersCordN, longitude: currentLong - metersCordN)
    }else if randomPM == 3 {
        return CLLocationCoordinate2D(latitude: currentLat - metersCordN, longitude: currentLong + metersCordN)
    }else if randomPM == 4 {
        return CLLocationCoordinate2D(latitude: currentLat, longitude: currentLong - metersCordN)
    }else {
        return CLLocationCoordinate2D(latitude: currentLat - metersCordN, longitude: currentLong)
    }

}

Second: Adding Random Annotation on all of the earth

erdekhayser Code to get a Random Float number

func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat {
    return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}

And then Calling:

func generateAnnoLoc() {

    var num = 0
    //First we declare While to repeat adding Annotation
    while num != 15 {
        num += 1

        //Add Annotation
        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(randomBetweenNumbers(-90, secondNum: 90)), longitude: CLLocationDegrees(randomBetweenNumbers(-180, secondNum: 180)))
        //-180 is the minimum of longitude and 180 is the maximum
        //-90 is the minimum of latitude and 90 is the maximum
        annotation.title = "Annotation Title"
        annotation.subtitle = "SubTitle"
        mapView.addAnnotation(annotation)

    }

}

Third: Adding Random Annotation in Specific Country

You only want the Bounding box of the country or city You can find it Here

this Will Generate Random Coordinates in Egypt:

CLLocationCoordinate2D(latitude: CLLocationDegrees(randomBetweenNumbers(22, secondNum: 24.698099)), longitude: CLLocationDegrees(randomBetweenNumbers(31.674179, secondNum: 36.89468))) 

Upvotes: 11

Mahmut Akyol
Mahmut Akyol

Reputation: 84

You can use random double values between coordinate values in the earth for example:

do{
    let latitude: CLLocationDegrees = randomLatitude()
    let longitude: CLLocationDegrees = randomLongtitude()
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longtitude)

    annotation = MKPointAnnotation()
    annotation.coordinate = location
    map.addAnnotation(annotation)
}while count == 15

P.S. Random value for swift you can look at this

Upvotes: -3

Related Questions