2015-07-12 33 views
0

我使用MapKit的localsearch功能來生成任何輸入到搜索欄中的註釋引腳。我現在無法找到這些固定位置的地址。這是我的本地搜索如何使用MapKit和Swift查找固定項目的位置

func performSearch() { 


    matchingItems.removeAll() 

    let request = MKLocalSearchRequest() 
    request.naturalLanguageQuery = searchText.text; 

    request.region = attractionsMap.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)") 

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

       var annotation = MKPointAnnotation() 
       annotation.coordinate = item.placemark.coordinate 
       annotation.title = item.name 
       self.attractionsMap.addAnnotation(annotation) 
      } 
     } 
    }) 
} 

代碼這是我的viewForAnnotation方法:

extension AttractionsVC: MKMapViewDelegate { 
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!.animatesDrop = true; 
    } 
    var moreInfoButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton; 
    pinView?.rightCalloutAccessoryView = moreInfoButton; 
    return pinView; 
} 

我現在想能夠在註釋顯示的地址。任何想法將不勝感激。

+0

你在MKMapViewDelegate中實現了mapVIew(_,viewForAnnotation :)嗎? – nRewik

+0

因此,這些引腳已添加到MapView中,而您找不到它? – nRewik

+0

我可以找到它,但我想能夠知道固定位置的地址。例如,如果我輸入咖啡,我會得到大約10場比賽,然後固定在地圖上。當我點擊圖釘時,它會顯示該地點的名稱,但我想要獲取該圖釘的位置以便在詳細的視圖控制器上顯示。 – kebabTiger

回答

1

我還是不明白你想要什麼。有時你會詢問地址,有時你會詢問地點。

的位置的經度和緯度

這是註釋的coordinate財產。

地址是人的郵政地址

當你打電話

let search = MKLocalSearch(request: request) 
search.startWithCompletionHandler(// ... 

...你回來MKMapItems:

for item in response.mapItems as! [MKMapItem] { 

裏面每個MKMapItem是一個地標(一MKPlacemark,MKMapItem的placemark屬性)。每個地標的內部都是地址信息 - MKPlacemark是CLPlacemark,因此請參閱CLPlacemark文檔以瞭解如何提取地址信息。

嗯,你附加每個MKMapItem到數組:

matchingItems.append(item as MKMapItem) 

這也正是MKMapItems是,除非你放棄你;所以這就是地址信息的地方。

+0

是的,但我如何獲得每個特定引腳的位置?我是否需要遍歷該數組或...? – kebabTiger

+1

你是一個程序員,你在想什麼。你是製作針腳的人。如果你知道你將要在晚些時候將地址與PIN關聯起來,請提前計劃!以某種方式聯繫他們! – matt