2017-03-13 9 views
0

静的なセルで作成したカスタム画像にマップ注釈を変更しようとしています。マップのアノテーションは機能しますが、マップ内の画像を配置しようとすると、その場所がズームされますが、画像は表示されません。誰もが、私はそれが私のイメージは素晴らしいだろう注釈として表示しないように引き起こしている間違ってやっているものを私に伝えることができる場合スウィフトの画像で地図注釈を設定する

 import UIKit 
      import MapKit 
      import CoreLocation 


     class ContactsTableViewController: UITableViewController, MKMapViewDelegate, CLLocationManagerDelegate { 
    } 

    override func viewDidLoad() { 
      super.viewDidLoad() 


     self.addressMap.delegate = self 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as! customCell 

      if let _ = currentAnnotation { 
       addressMap.removeAnnotation(currentAnnotation!) 
      } 


      let contact = contacts.contactWithIndex((indexPath as NSIndexPath).row) 
      print(contact) 
      cell.nameLabel.text = contact.name 
      cell.addressLabel.text = contact.address 

      let country = "USA" 
      let city = "Cicero" 
      let street = contact.address 

      // Create Address String 
      let address = "\(country), \(city), \(street)" 

      // Geocode Address String 
      geocoder.geocodeAddressString(address) { (placemarks, error) in 
       // Process Response 
       self.processResponse(withPlacemarks: placemarks, error: error) 
       var location: CLLocation? 

       if let placemarks = placemarks, placemarks.count > 0 { 
        location = placemarks.first?.location 
       } 

       if let location = location { 
        let coordinate = location.coordinate 
        print("Custom cell: \(coordinate.latitude), \(coordinate.longitude)") 

        //Drops pin to current MKPointAnnotationis logged in 
        self.currentAnnotation = MKPointAnnotation() 
        self.currentAnnotation?.coordinate = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) 

        let image1 = CustomPointAnnotation() 
        image1.imageName = "mapPin.png" 
        self.addressMap.addAnnotation(image1) 

        //Pins the location on the mapview 
        //cell.addressMap.addAnnotation(self.currentAnnotation!) 
        cell.addressMap.addAnnotation(image1) 
        //Auto zoom into user location 
        let span = MKCoordinateSpanMake(0.02, 0.02) 
        let region = MKCoordinateRegion(center: (self.currentAnnotation?.coordinate)!, span: span) 
        cell.addressMap.setRegion(region, animated: true) 

       } else { 
        print("No Matching Location Found") 
       } 
      } 
    } 


func addressMap(_ addressMap: MKMapView, viewFor annotation: MKAnnotation!) -> MKAnnotationView? { 
     if !(annotation is CustomPointAnnotation) { 
      return nil 
     } 

     let reuseId = "test" 
     var anView = addressMap.dequeueReusableAnnotationView(withIdentifier: reuseId) 
     if anView == nil { 
      anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView?.canShowCallout = true 
     } else { 
      anView?.annotation = annotation 
     } 

     //Set annotation-specific properties **AFTER** 
     //the view is dequeued or created... 

     let cpa = annotation as! CustomPointAnnotation 
     anView?.image = UIImage(named:cpa.imageName) 

     return anView 
     } 

ここで注釈

import UIKit 
import MapKit 

class CustomPointAnnotation: MKPointAnnotation { 
    var imageName: String! 

} 

ためのクラスです。

+0

あなたは 'CustomPointAnnotation'の' coordinate'を決して設定していないようです。 – Rob

+0

'coordinate'を' image1.coordinate = CLLocationCoordinate2Make(42、-84) 'に設定すると、元のピンが表示され、私の' 'mapPin.png ''は表示されません –

答えて

0

問題のカップル:

  1. あなたは今までにCustomPointAnnotationcoordinateを設定するためには表示されません。

  2. mapView(_:viewFor:)の署名が正しくありません。メソッドにブレークポイントを追加すると、ブレークポイントが呼び出されていないことがわかります。あなたのマップアウトレットはaddressMapと呼ばれるかもしれませんが、MKMapViewコンセントリファレンスの名前にかかわらず、デリゲートメソッド名はmapView(_:viewFor:)です。

関連する問題