2015-05-05 55 views
6

我的應用程序當前有一個本地搜索,爲搜索結果添加註釋。我想在選擇註釋並單擊呼出按鈕的地方進行設置,它將在地圖應用程序中打開,並指示當前設備位置的註釋。我遇到了一些問題。Swift - 從當前位置在地圖中選擇註釋的方向

首先,我沒有出現註釋按鈕。其次,我不認爲我正在檢測選定的註釋。

這裏是我的搜索和選擇的註釋代碼:

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

} 

任何幫助將提前讚賞,感謝。

回答

6

對於第一個問題:必須在viewForAnnotation委託方法(默認紅色圖釘沒有之一)明確設定

標註按鈕。這裏是一個可能實現的一個簡單的例子:

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 
} 


對於第二個問題:

首先,在calloutAccessoryControlTapped,使用view.annotation所以使用selectedAnnotations陣列中有不必要的註釋是直接訪問。

接下來,openMapsWithItems預計MKMapItem對象的數組,但數組中您傳遞([selectedLoc, currentLoc]),selectedLocMKMapItem - 它只是一些對象實現MKAnnotation

運行這段代碼將導致此錯誤崩潰:

-[MKPointAnnotation dictionaryRepresentation]: unrecognized selector sent to instance

在地圖應用嘗試使用selectedLoc,就好像它是一個MKMapItem

相反,您需要從selectedLoc註釋創建一個MKMapItem。這可以通過首先使用MKPlacemark(coordinate:addressDictionary:)從註釋創建MKPlacemark,然後使用MKMapItem(placemark:)從地標創建MKMapItem來完成。

例子:

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

不知怎的,我downvoted這個答案,甚至不知道(有人用我的手機打)。我爲此道歉。現在我無法撤消。如果你想要,你可以做一個編輯,這樣我可以恢復這個。 –

+1

@flashadvanced:沒問題,謝謝你解釋。 – Anna

+0

非常感謝您的幫助!我的最後一個問題是......我如何將視圖分配給'performSearch()'中創建的註釋?添加代碼並運行後,註釋視圖仍然是默認的紅色引腳。 – Alec