2016-12-27 117 views
0
var lat:CLLocationDegrees = 40.748708 
    var long:CLLocationDegrees = -73.985643 
    var latDelta:CLLocationDegrees = 0.01 
    var longDelta:CLLocationDegrees = 0.01 

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta) 
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long) 
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span) 

    mapView.setRegion(region, animated: true) 


    var information = MKPointAnnotation() 
    information.coordinate = location 
    information.title = "Test Title!" 
    information.subtitle = "Subtitle" 



    mapView.addAnnotation(information) 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 


    if !(annotation is MKPointAnnotation) { 
     return nil 
    } 

    let reuseId = "test" 

    var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
    if anView == nil { 
     anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     anView?.image = UIImage(named:"annotation") 
     anView?.canShowCallout = true 
    } 
    else { 
     anView?.annotation = annotation 
    } 

    return anView 
} 

我需要爲Mapview中的註釋加載自定義圖像。我有一個名爲「Annotation」的圖像,我試圖在註釋方法中調用它。我怎樣才能做到這一點?如何將自定義圖像添加到Mapview Swift ios中的註釋?

+0

這可能有助於全力爲您http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation –

+0

@ Miteshjadav我使用了相同的方法。但形象仍然沒有改變。請幫忙 – Rakesh

回答

1

下面是答案:

斯威夫特3:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !(annotation is MKUserLocation) else { 
     return nil 
    } 

    // Better to make this class property 
    let annotationIdentifier = "AnnotationIdentifier" 

    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 { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 

雨燕2.2:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    // Don't want to show a custom image if the annotation is the user's location. 
    guard !annotation.isKindOfClass(MKUserLocation) else { 
     return nil 
    } 

    let annotationIdentifier = "AnnotationIdentifier" 

    var annotationView: MKAnnotationView? 
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) { 
     annotationView = dequeuedAnnotationView 
     annotationView?.annotation = annotation 
    } 
    else { 
     let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
     av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
     annotationView = av 
    } 

    if let annotationView = annotationView { 
     // Configure your annotation view here 
     annotationView.canShowCallout = true 
     annotationView.image = UIImage(named: "yourImage") 
    } 

    return annotationView 
} 
+0

圖片尚未加載。 – Rakesh

+0

@Rakesh您正在使用哪個版本的swift?也可以檢查圖片資源中的圖片出口。因爲我也檢查了上面的代碼,它顯示了我的圖像 – Hrishikesh

+0

我正在使用swift3。我的形象存在於資產中。 – Rakesh

0

實現這一目標是使用自定義的類來存儲註解的最好方法。

import UIKit 
import MapKit 
import CoreLocation 

class Annotation: NSObject, MKAnnotation { 

var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D() 
var title: String? 
var subtitle: String? 

var strTitle = "" 
var strImgUrl = "" 
var strDescr = "" 


init(coordinates location: CLLocationCoordinate2D, title1: String, description: String, imgURL: String) { 
    super.init() 

    coordinate = location 
    title = title1 
    subtitle = description 
    strTitle = title1 
    strImgUrl = imgURL 
    strDescr = description 
} 
} 

現在使用此類來存儲註釋並填充您的針腳。

// MapViewController.swift 
let myAnnotation1: Annotation = Annotation.init(coordinates: CLLocationCoordinate2D.init(latitude: 30.733051, longitude: 76.763042), title1: "The Mayflower Renaissance Hotel", description: "The Histroic hotel has been the site of saveral dramatic locations.", imgURL: "custom.jpg") 
    self.uvMApView.addAnnotation(myAnnotation1) 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if (annotation is MKUserLocation) { 
     return nil 
    } 
    // try to dequeue an existing pin view first 
    let AnnotationIdentifier = "AnnotationIdentifier" 

    let myAnnotation1 = (annotation as! Annotation) 

    let pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: AnnotationIdentifier) 
    pinView.canShowCallout = true 
    pinView.image = UIImage(named: myAnnotation1. strImgUrl)! 
    return pinView 

    } 
+0

尚未加載圖像@Ankit Saini – Rakesh

+0

您試圖加載的圖像在您的應用程序包中,或者您從某個網址加載它?如果這是服務器映像,那麼您必須替換{pinView.image = UIImage(名爲:myAnnotation1。strImgUrl)! }。 –

+0

它在我的項目。 – Rakesh

0

你也可以這樣做:

let reuseId = "pin" 
    var your_pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    let your_image = UIImage (named: "your_image") 

    pinView?.image = your_image 
    pinView?.canShowCallout = true 

我認爲這是比較容易回答。

0

夫特4.0

var coordinates: [[Double]]! 
var addresses:[String]! 

在viewDidLoad中()添加以下代碼來添加註解MapvView。

coordinates = [[28.4344,72.5401],[28.85196,72.3944],[28.15376,72.1953]]// Latitude,Longitude 
    addresses = ["Address1","Address2","Address3"] 
    self.mapView.delegate = self // set MapView delegate to self 
    for i in 0...2 { 
     let coordinate = coordinates[i] 
     let point = CustomAnnotation(coordinate: CLLocationCoordinate2D(latitude: coordinate[0] , longitude: coordinate[1])) // CustomAnnotation is class and pass the required param 
     point.address = addresses[i] // passing only address to pin, you can add multiple properties according to need 
     self.mapView.addAnnotation(point) 
    } 
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 28.856614, longitude: 72.3522219), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)) 
    self.mapView.setRegion(region, animated: true) 

添加從MKAnnotation繼承其名稱CustomAnnotation新類爲我們定製標註

import MapKit 
class CustomAnnotation: NSObject, MKAnnotation { 
    var coordinate: CLLocationCoordinate2D 
    var address: String! 
    init(coordinate: CLLocationCoordinate2D) { 
     self.coordinate = coordinate 
    } 
} 

添加從MKAnnotationView繼承其名稱AnnotationView一個多類。我顯示自定義標註,因此它需要重寫overridefuncpoint()方法以在自定義標註視圖中接收觸摸。

import MapKit 
class AnnotationView: MKAnnotationView 
{ 
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
     let hitView = super.hitTest(point, with: event) 
     if (hitView != nil) 
     { 
      self.superview?.bringSubview(toFront: self) 
     } 
     return hitView 
    } 
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 
     let rect = self.bounds; 
     var isInside: Bool = rect.contains(point); 
     if(!isInside) 
     { 
      for view in self.subviews 
      { 
       isInside = view.frame.contains(point); 
       if isInside 
       { 
        break; 
       } 
      } 
     } 
     return isInside; 
    } 
} 

現在實行viewForannotation MapView的委託作爲跟隨

extension YourViewC: MKMapViewDelegate { 

     func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
      if annotation is MKUserLocation 
      { 
       return nil 
      } 
      var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "CustomPin") 
      if annotationView == nil{ 
       annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "CustomPin") 
       annotationView?.canShowCallout = false 
      }else{ 
       annotationView?.annotation = annotation 
      } 
      annotationView?.image = UIImage(named: "bluecircle") // Pass name of your custom image 
      return annotationView 
     } 
    func mapView(_ mapView: MKMapView, 
       didSelect view: MKAnnotationView){ 
     let annotation = view.annotation as? CustomAnnotation 
     print(annotation?.address) // get info you passed on pin 

     // write code here to add custom view on tapped annotion 
} 
相關問題