2015-05-05 29 views
6

Ứng dụng của tôi hiện đang có tìm kiếm địa phương bổ sung chú thích cho kết quả tìm kiếm. Tôi muốn thiết lập nó khi bạn chọn chú thích và nhấp vào nút gọi ra, nó sẽ mở trong ứng dụng Bản đồ với hướng dẫn đến chú thích từ vị trí thiết bị hiện tại. Tôi đang gặp một vài vấn đề với điều này.Swift - Chỉ đường đến Chú thích được Chọn từ Vị trí Hiện tại trong Bản đồ

Trước hết, nút gọi ra của tôi trên chú thích không xuất hiện. Thứ hai, tôi không nghĩ rằng tôi đang phát hiện chú thích được chọn một cách chính xác.

Đây là mã của tôi cho các chú thích tìm kiếm và chọn:

func performSearch() { 

    matchingItems.removeAll() 
    let request = MKLocalSearchRequest() 
    request.naturalLanguageQuery = searchText.text 
    request.region = mapView.region 

    let search = MKLocalSearch(request: request) 

    search.startWithCompletionHandler({(response: 
     MKLocalSearchResponse!, 
     error: NSError!) in 

     if error != nil { 
      println("Error occured in search: \(error.localizedDescription)") 
     } else if response.mapItems.count == 0 { 
      println("No matches found") 
     } else { 
      println("Matches found") 

      for item in response.mapItems as! [MKMapItem] { 
       println("Name = \(item.name)") 
       println("Phone = \(item.phoneNumber)") 

       self.matchingItems.append(item as MKMapItem) 
       println("Matching items = \(self.matchingItems.count)") 

       var annotation = MKPointAnnotation() 
       var coordinates = annotation.coordinate 
       annotation.coordinate = item.placemark.coordinate 
       annotation.title = item.name 
       self.mapView.addAnnotation(annotation) 

      } 
     } 
    }) 
} 



func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
    calloutAccessoryControlTapped control: UIControl!) { 

     if self.mapView.selectedAnnotations?.count > 0 { 

      if let selectedLoc = self.mapView.selectedAnnotations[0] as? MKAnnotation { 
       println("Annotation has been selected") 
       let currentLoc = MKMapItem.mapItemForCurrentLocation() 
       let mapItems = NSArray(objects: selectedLoc, currentLoc) 
       let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] 
       MKMapItem.openMapsWithItems([selectedLoc, currentLoc], launchOptions: launchOptions) 
      } 
     } 

} 

Bất kỳ trợ giúp sẽ được đánh giá cao, cảm ơn trước.

Trả lời

6

Đối với vấn đề đầu tiên: nút

Một callout cần phải được thiết lập một cách rõ ràng trong phương pháp viewForAnnotation đại biểu (chân mặc định màu đỏ không có). Dưới đây là một ví dụ đơn giản của một thực thể:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

    if annotation is MKUserLocation { 
     return nil 
    } 

    let reuseId = "pin" 

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 
     pinView!.pinColor = .Purple 

     //next line sets a button for the right side of the callout... 
     pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIButton 
    } 
    else { 
     pinView!.annotation = annotation 
    } 

    return pinView 
} 


Đối với vấn đề thứ hai:

Thứ nhất, trong calloutAccessoryControlTapped, chú thích là truy cập trực tiếp sử dụng view.annotation nên sử dụng các mảng selectedAnnotations trong đó là không cần thiết .

Tiếp theo, openMapsWithItems hy vọng một mảng của MKMapItem đối tượng nhưng trong mảng bạn đang đi qua ([selectedLoc, currentLoc]), selectedLockhông một MKMapItem - nó chỉ là một số đối tượng mà thực hiện MKAnnotation.

Chạy đoạn mã này sẽ dẫn đến một vụ tai nạn với lỗi này:

-[MKPointAnnotation dictionaryRepresentation]: unrecognized selector sent to instance

khi ứng dụng Maps cố gắng sử dụng selectedLoc như thể nó là một MKMapItem. Thay vào đó, bạn cần tạo một MKMapItem từ chú thích selectedLoc. Điều này có thể được thực hiện trước tiên bằng cách tạo một MKPlacemark từ chú thích bằng cách sử dụng MKPlacemark(coordinate:addressDictionary:) và sau đó tạo một MKMapItem từ dấu vị trí bằng cách sử dụng MKMapItem(placemark:).

Ví dụ:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
    calloutAccessoryControlTapped control: UIControl!) { 

     let selectedLoc = view.annotation 

     println("Annotation '\(selectedLoc.title!)' has been selected") 

     let currentLocMapItem = MKMapItem.mapItemForCurrentLocation() 

     let selectedPlacemark = MKPlacemark(coordinate: selectedLoc.coordinate, addressDictionary: nil) 
     let selectedMapItem = MKMapItem(placemark: selectedPlacemark) 

     let mapItems = [selectedMapItem, currentLocMapItem] 

     let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] 

     MKMapItem.openMapsWithItems(mapItems, launchOptions:launchOptions) 
} 
+1

Bằng cách nào đó tôi downvoted câu trả lời này mà không biết (có ai đó đang chơi với điện thoại của tôi). Tôi xin lỗi vì điều đó. Bây giờ tôi không thể hoàn tác. Nếu bạn muốn, bạn có thể chỉnh sửa để tôi có thể hoàn nguyên. –

+1

@flashadvanced: Không sao cả, cảm ơn vì đã giải thích điều đó. – Anna

+0

Cảm ơn bạn rất nhiều vì đã giúp đỡ! Câu hỏi cuối cùng của tôi là ... Làm cách nào tôi có thể gán chế độ xem cho các chú thích được tạo trong 'performSearch()'? Sau khi thêm mã và chạy, chế độ xem chú thích vẫn là ghim màu đỏ mặc định. – Alec

Các vấn đề liên quan