2017-01-23 2 views
2
@IBOutlet weak var map: MKMapView! 
override func viewDidLoad() { 
    map.delegate = self 
    showAlarms() 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

func showAlarms(){ 

    map.region.center.latitude = 10.733051 
    map.region.center.longitude = 76.763042 
    map.region.span.latitudeDelta = 1 
    map.region.span.longitudeDelta = 1 


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

    if annotation is MKUserLocation { 
     //return nil so map view draws "blue dot" for standard user location 
     return nil 
    } 

    let reuseId = "pin" 
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 
     //pinView!.animatesDrop = true 
     pinView!.image = UIImage(named:"annotation")! 

    } 
    else { 
     pinView!.annotation = annotation 
    } 

    return pinView 
} 

내지도보기에 맞춤 핀 이미지를 표시해야합니다. 맞춤 이미지가 핀을로드하지 않습니다. mapview에로드해야하는 "주석"이라는 이미지가 있습니다.Mapview swift ios에서 맞춤 핀 이미지를 설정하는 방법은 무엇입니까?

+0

viewForAnnotation의 대리자 메서드가 호출되었거나 호출되지 않았습니다. –

+0

@KiritModi. 나는 그렇게 생각하지 않는다. 지도 만 보이고 다른 것은 보이지 않습니다. 도와주세요. – Rakesh

+0

당신은 신속한 코드 3 –

답변

3
mapview.delegate = self 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
guard !(annotation is MKUserLocation) else { 
    return nil 
} 

let annotationIdentifier = "Identifier" 
var annotationView: MKAnnotationView? 
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 
    annotationView = dequeuedAnnotationView 
    annotationView?.annotation = annotation 
} 
else { 
    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
    annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
} 

if let annotationView = annotationView { 

    annotationView.canShowCallout = true 
    annotationView.image = UIImage(named: "yourImagename”) 
} 
    return annotationView 
} 
0

또한 사용자 정의 MKAnnotationView을 만들고 내부의 이미지를 설정할 수 있습니다

class myCustomAnnotationView : MKAnnotationView { 

    override var annotation: MKAnnotation? { 
    willSet { 
     guard let annotation = newValue else {return} 
     switch annotation { 
     case is myCustomMKAnnotation: 
      self.canShowCallout = true 
      self.image = #yourimage 
      self.centerOffset = CGPoint(x: 0, y: -self.image!.size.height/2) 
      break 
     default: 
      return 
     } 
     self.setAnimationLayer() 
    } 
} 

를 그리고 모든 당신이 그것을 할 필요가 :

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { return nil } 
    guard !(annotation is MKUserLocation) else { 


    let annotationIdentifier = "Identifier" 
    if let myAnnotation = annotation as? myCustomMKAnnotation { 
     let annotation = myCustomAnnotationView(annotation: siteAnnotation, reuseIdentifier: annotationIdentifier) 
     return annotation 
    } 
    return nil 
} 

을 나는 그것이 청소기처럼 보이는 생각

관련 문제