func getDirections(enterdLocations:[String]) {
let geocoder = CLGeocoder()
// array has the address strings
for (index, item) in enterdLocations.enumerated() {
geocoder.geocodeAddressString(item, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
let dropPin = MKPointAnnotation()
dropPin.coordinate = coordinates
dropPin.title = item
self.myMapView.addAnnotation(dropPin)
self.myMapView.selectAnnotation(dropPin, animated: true)
}
})
}
}
내 호출 기능을 하나의
let geocoder = CLGeocoder()
, 그래서 그냥
for
루프에 그 이동하고과 같이 작동합니다 :
func getDirections(enterdLocations:[String]) {
// array has the address strings
var locations = [MKPointAnnotation]()
for item in enterdLocations {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(item, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
let dropPin = MKPointAnnotation()
dropPin.coordinate = coordinates
dropPin.title = item
self.myMapView.addAnnotation(dropPin)
self.myMapView.selectAnnotation(dropPin, animated: true)
locations.append(dropPin)
//add this if you want to show them all
self.myMapView.showAnnotations(locations, animated: true)
}
})
}
}
모든 주석을 저장할 위치 var locations
배열을 추가하여 self.myMapView.showAnnotations(locations, animated: true)
...으로 모두 표시 할 수 있습니다. 필요가 없다면 묵으십시오.
고마워요. 배열에있는 핀들 사이의 경로를 그려 줄 수 있습니까? –
다음을 살펴보십시오. https://www.hackingwithswift.com/example-code/location/how-to-find-directions-using-mkmapview-and-mkdirectionsrequest – Ladislav