2017-02-05 10 views
1

MKMapViewを使用して会社の所在地を表示するアプリがあります。ユーザーが連絡先ページにアクセスすると、画面上部にピンがドロップしている場所の地図が表示され、その場所が表示されます。地図ピンをクリックすると、注釈に会社名が表示されます。Swift:ピン注釈に方向ボタンを追加

backkappを使用してParseバックエンドに格納されたデータから会社のアドレスをMKMApViewにプルしています。

地図のピンをクリックすると会社名が表示されますが、そこに車のアイコンボタンも表示されていますので、ユーザーが会社に向かうことができます。ここで

は私が注釈を設定する必要があるコード:

func addPinOnMap(_ address: String) { 
    var hotelClass = PFObject(className: HOTEL_CLASS_NAME) 
    hotelClass = hotelArray[0] 

    if mapView.annotations.count != 0 { 
     annotation = mapView.annotations[0] 
     mapView.removeAnnotation(annotation) 
    } 

    // Make a search on the Map 
    localSearchRequest = MKLocalSearchRequest() 
    localSearchRequest.naturalLanguageQuery = address 
    localSearch = MKLocalSearch(request: localSearchRequest) 

    localSearch.start { (localSearchResponse, error) -> Void in 

     // Add PointAnnonation text and a Pin to the Map 
     self.pointAnnotation = MKPointAnnotation() 
     self.pointAnnotation.title = "\(hotelClass[HOTEL_NAME]!)" 
     self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:localSearchResponse!.boundingRegion.center.longitude) 

     self.pinView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil) 
     self.mapView.centerCoordinate = self.pointAnnotation.coordinate 
     self.mapView.addAnnotation(self.pinView.annotation!) 

     // Zoom the Map to the location 
     self.region = MKCoordinateRegionMakeWithDistance(self.pointAnnotation.coordinate, 1000, 1000); 
     self.mapView.setRegion(self.region, animated: true) 
     self.mapView.regionThatFits(self.region) 
     self.mapView.reloadInputViews() 
    } 

} 

私は単に私が注釈にボタンアイコンを取得し、ボタンのクリック時に方向性を示すことができるかわかりません。現在の地図ビューを使用するか、端末のユーザーマップアプリを開くにはどちらの方法でも問題ありません。

ありがとうございます。

答えて

2

ピンにボタンがあり、リンゴマップアプリケーションでピンの方向をユーザーに伝えるサンプルメソッドを提案します。

最初に、ピンの宣言の後、ピンの魔法使いを宣言して、ピンでユーザーとやり取りする機会を与えてください(後でピンのボタンを初期化する方法を示します)。あなたはこのようなあなたのボタンを宣言することができ

let smallSquare = CGSize(width: 30, height: 30) 
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare)) 

smallSquareはボタンにあなたの将来のピンのボタンの正しいサイズを与えます。


ピンのボタンに画像を追加する方法は、button.setBackgroundImage(UIImage(named: "car"), forState: .Normal)です。

アップルの開発者には、いくつかのドキュメントを与えてください:Apple developer web site


次に、この方法でボタン操作を行うことができます:button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside)。 このように、ボタンが正常に選択されている場合(.TouchUpInside)、プログラムはgetDirection()関数を呼び出します。


ここで、ピンのボタンは、pinView?.leftCalloutAccessoryView = buttonで初期化できます。

このメソッドは、ピンの左側にあるボタンを設定します。このようにボタンを右に置くことができます:pinView?.rightCalloutAccessoryView = button

Apple developer件名に関する情報を入力してください。

しかし、ピンの全面にあるボタンをどのように初期化するかわかりません。


最後に、あなたは利用者に、ピンの「方向」を与える機能getDirections()を持っている必要があります。

私はこの目的のためにリンゴマップアプリを使用することを提案します。

My機能getDirections()は、次のようになります。

func getDirections(){ 
     guard let selectedPin = selectedPin else { return } 
     let mapItem = MKMapItem(placemark: selectedPin) 
     let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] 
     mapItem.openInMapsWithLaunchOptions(launchOptions) 

     print ("GET DIRECTION") 
    } 

私はこのweb siteでこの機能を見つけます。私はそれが私よりもこれを説明できると思う。終わりに、私のプログラムは次のようになります

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

    guard !(annotation is MKUserLocation) else { return nil } 
    let reuseId = "pin" 
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
    } 
    pinView?.pinTintColor = UIColor.orangeColor() 
    pinView?.canShowCallout = true 
    //The initialization of the pin. Here your program looks great. 

    // And after the code as seen above. 
    let smallSquare = CGSize(width: 30, height: 30) 
    let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare)) 
    button.setBackgroundImage(UIImage(named: "car"), forState: .Normal) 
    button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside) 
    pinView?.leftCalloutAccessoryView = button 

    return pinView 
} 




func getDirections(){ 
    guard let selectedPin = selectedPin else { return } 
    let mapItem = MKMapItem(placemark: selectedPin) 
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] 
    mapItem.openInMapsWithLaunchOptions(launchOptions) 

    print ("GET DIRECTION") 
} 

通常、あなたのプログラムは次のようになります。

func addPinOnMap(_ address: String) {  

     var hotelClass = PFObject(className: HOTEL_CLASS_NAME) 
     hotelClass = hotelArray[0] 

     if mapView.annotations.count != 0 { 
      annotation = mapView.annotations[0] 
      mapView.removeAnnotation(annotation) 
     } 

     // Make a search on the Map 
     localSearchRequest = MKLocalSearchRequest() 
     localSearchRequest.naturalLanguageQuery = address 
     localSearch = MKLocalSearch(request: localSearchRequest) 

     localSearch.start { (localSearchResponse, error) -> Void in 

      // Add PointAnnonation text and a Pin to the Map 
      self.pointAnnotation = MKPointAnnotation() 
      self.pointAnnotation.title = "\(hotelClass[HOTEL_NAME]!)" 
      self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:localSearchResponse!.boundingRegion.center.longitude) 

      self.pinView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil) 
      self.mapView.centerCoordinate = self.pointAnnotation.coordinate 
      self.mapView.addAnnotation(self.pinView.annotation!) 

      let smallSquare = CGSize(width: 30, height: 30) 
      let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare)) 
      button.setBackgroundImage(UIImage(named: "car"), forState: .Normal) 
      button.addTarget(self, action: #selector(ViewController.getDirections), forControlEvents: .TouchUpInside) 
      self.pinView?.leftCalloutAccessoryView = button 

      // Zoom the Map to the location 
      self.region = MKCoordinateRegionMakeWithDistance(self.pointAnnotation.coordinate, 1000, 1000); 
      self.mapView.setRegion(self.region, animated: true) 
      self.mapView.regionThatFits(self.region) 
      self.mapView.reloadInputViews() 
     } 

func getDirections(){ 
guard let selectedPin = selectedPin else { return } 
let mapItem = MKMapItem(placemark: selectedPin) 
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] 
mapItem.openInMapsWithLaunchOptions(launchOptions) 

print ("GET DIRECTION") 
} 

は、すみません、私はあなたの持っていないので、私はあなたのコードを試すことができません完全にpinView宣言のようなコードです。

注:注:このコード行を受け入れるには、ピンがコールアウトpinView?.canShowCallout = trueを示すことができるようにする必要があります。

詳細や詳細については、このWebサイトを参照してください。

Thorn technologies

Apple developer

+1

あなたのアドバイスは優秀だった、これはすべて私の場合のために働いていました!私を助けるために時間を割いていただきありがとうございます、私は今、私の地図のピンのボタンから正常に方向を得ることができます! –

関連する問題