0

MKMapView の表示部分に 10 個のカスタム ピンを表示するコードがあります。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if !(annotation is MKPointAnnotation) {
        return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "demo")
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "demo")
        annotationView!.canShowCallout = true
    }
    else {
        annotationView!.annotation = annotation
    }

    annotationView!.image = UIImage(named: "OtherPin")
    return annotationView

} 

ランダム ピンを生成する関数は次のとおりです。

func addPinsToMap(mapView: MKMapView, amount howMany:Int) {

//First we need to calculate the corners of the map so we get the points
    let nePoint:CGPoint = CGPoint(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
    let swPoint:CGPoint = CGPoint((mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
    let neCoord:CLLocationCoordinate2D = mapView.convert(nePoint, toCoordinateFrom: mapView)
    let swCoord:CLLocationCoordinate2D = mapView.convert(swPoint, toCoordinateFrom: mapView)

// Loop
for _ in 0 ..< howMany {

    let latRange:Double = Double(self.randomBetweenNumbers(firstNum: CGFloat(neCoord.latitude), secondNum: CGFloat(swCoord.latitude)))
    let longRange:Double = Double(self.randomBetweenNumbers(firstNum: CGFloat(neCoord.longitude), secondNum: CGFloat(swCoord.longitude)))

// Add new waypoint to map
    let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latRange, longRange);

    let annotation = MKPointAnnotation()
    //let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29)
    annotation.coordinate = location
    annotation.title = "Title"
    mapView.addAnnotation(annotation)

}//end

}//end

上記のコードでユーザーがマップを開くと、ピンがすでにランダムな場所に配置されていることがわかります。私の質問は、ピンを次々と落とすにはどうすればよいですか、可能であればゆっくりと落とすことができますか?

4

1 に答える 1