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

时间:2016-07-12 10:56:20

标签: ios swift xcode mapkit

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

Or

2 个答案:

答案 0 :(得分: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)

    }

}

生成坐标:

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

}

第二:在地球上添加随机通知

erdekhayser Code获取随机浮点数

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

然后打电话:

    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)

                }

            }

第三:在特定国家添加随机骚动

您只需要国家或城市的边界框即可找到它Here

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

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

感谢,,

答案 1 :(得分:-3)

您可以在地球中的坐标值之间使用随机双精度值,例如:

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。 swift的随机值你可以查看this