如何在Mapkit Swift中随意放置注释

Moh*_*lah 3 xcode mapkit ios swift

我想知道如何在MapView中随机地固定15个注释

    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) 
Run Code Online (Sandbox Code Playgroud)

Moh*_*lah 9

对于任何想要解决方案的人:

第一:根据当前位置添加随机Annontation

生成注释:

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)

    }

}
Run Code Online (Sandbox Code Playgroud)

生成坐标:

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)
    }

}
Run Code Online (Sandbox Code Playgroud)

第二:在地球上添加随机骚扰

erdekhayser代码获取随机浮点数

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

然后打电话:

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)

    }

}
Run Code Online (Sandbox Code Playgroud)

第三:在特定国家增加随机骚扰

你只想要国家或城市的边界框你可以在这里找到它

这将在埃及产生随机坐标:

CLLocationCoordinate2D(latitude: CLLocationDegrees(randomBetweenNumbers(22, secondNum: 24.698099)), longitude: CLLocationDegrees(randomBetweenNumbers(31.674179, secondNum: 36.89468))) 
Run Code Online (Sandbox Code Playgroud)

谢谢,,