2012-03-22 27 views
54

Tôi muốn hiển thị hình ảnh trong MKMapView thay vì ít mã pin đá. Có thể ai đó xin vui lòng đặt một số mã hữu ích ở đây, hoặc nói cách làm thế nào để làm điều đó?MKMapView: Thay vì Pin chú thích, chế độ xem tùy chỉnh

Cảm ơn!

EDIT

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: 
(id <MKAnnotation>)annotation { 
MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"com.invasivecode.pin"; 
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) pinView = [[MKPinAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

    pinView.pinColor = MKPinAnnotationColorGreen; 
    pinView.canShowCallout = YES; 
    pinView.animatesDrop = YES; 
    pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
} 
else { 
    [mapView.userLocation setTitle:@"I am here"]; 
} 
return pinView; 
} 

Tôi đang mong đợi của tôi hình ảnh pinks.jpg được trên bản đồ, ghim vị trí thay vì xem pin mặc định (pin đá hình). Nhưng tôi vẫn nhận được hình ảnh mặc định của pin.

+0

Có vẻ như bạn muốn chú thích tùy chỉnh. Kiểm tra [hướng dẫn này] (http://shawnsbits.com/blog/2011/04/12/custom-map-pins-for-mapkit/). Googling cho hướng dẫn chú thích tùy chỉnh cũng sẽ giúp bạn thực hiện nếu hướng dẫn đó không phù hợp với bạn. – Squatch

+0

Tôi đoán Chú thích chỉ là _note_ được đặt để cho biết thông tin về vị trí _pinned_. Nhưng tôi muốn tùy chỉnh ghim, thay đổi hình ảnh của nó thành logo của công ty. – turtle

+0

Tôi cũng đã thử bạn _tutorial_ cách, có vẻ như nó không làm việc cho tôi. Hướng dẫn cài đặt thuộc tính 'image' của đối tượng' MKAnnotationView' thành 'UIImage'. Điều này tôi đã thử nghiệm, không thay đổi _pinView_ thành hình ảnh được chỉ định của tôi. – turtle

Trả lời

112

Khi bạn muốn sử dụng hình ảnh của riêng mình cho chế độ xem chú thích, bạn nên tạo MKAnnotationView thay vì MKPinAnnotationView.

MKPinAnnotationView là lớp con của MKAnnotationView để nó có thuộc tính image nhưng nó thường ghi đè và vẽ hình ảnh ghim (đó là những gì nó dành cho).

Vì vậy, thay đổi mã để:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    { 
     static NSString *defaultPinID = @"com.invasivecode.pin"; 
     pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (pinView == nil) 
      pinView = [[MKAnnotationView alloc] 
             initWithAnnotation:annotation reuseIdentifier:defaultPinID]; 

     //pinView.pinColor = MKPinAnnotationColorGreen; 
     pinView.canShowCallout = YES; 
     //pinView.animatesDrop = YES; 
     pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch 
    } 
    else { 
     [mapView.userLocation setTitle:@"I am here"]; 
    } 
    return pinView; 
} 


ý rằng animatesDrop cũng là nhận xét ra kể từ khi tài sản đó chỉ tồn tại trong MKPinAnnotationView.

Nếu bạn vẫn muốn chú thích hình ảnh của mình thả xuống, bạn sẽ phải tự tạo hoạt ảnh. Bạn có thể tìm kiếm Stack Overflow cho "animatesdrop mkannotationview" và bạn sẽ tìm thấy một số câu trả lời. Dưới đây là lần đầu tiên hai:

+0

Xin chào Anna, tôi chỉ muốn thêm sự kiện khi nhấp vào hình ảnh pin này. Làm thế nào tôi có thể làm điều đó? – turtle

+0

Xin chào Anna, bạn có ý tưởng nào trong tâm trí của bạn cho câu hỏi sau đây http://stackoverflow.com/questions/27489898/image-annotation-on-ios – casillas

+0

@Anna hãy giúp tôi trên http://stackoverflow.com này/câu hỏi/35647839/show-dynamic-annotation-pin-in-mapview –

17

Dưới đây là một câu trả lời trên Swift 3. Nó dequeues xem chú thích nếu có thể hoặc tạo ra một cái mới nếu không muốn nói:

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 
} 

Swift 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 
    } 

    // Better to make this class property 
    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

Cảm ơn bạn. Tại sao bạn gọi 'annotationView? .annotation = annotation'? Tôi nghĩ rằng chú thích và chế độ xem được liên kết tự động? – Andrej

+1

Chúng không được liên kết tự động. Bạn đang dequeueing một cái nhìn chú thích tái sử dụng. Nếu nó được sử dụng trước khi nó có thể có một 'chú thích' được liên kết. –

0

Tôi đồng ý với với câu trả lời của Anna và tôi muốn hiển thị như thế nào sẽ xem xét rằng trong swift3. Câu trả lời này với nhiều tùy chọn khác. Giống như thay đổi kích thước hình ảnh, nhận danh sách hình ảnh từ mảng và ect.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if let annotation = annotation as? PetrolStation { 
      let identifier = "pinAnnotation" 
      var view: MKAnnotationView 
      if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) 
       as? MKPinAnnotationView { // 2 
       dequeuedView.annotation = annotation 
       view = dequeuedView 
      } else { 
       // 3 
       view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       view.canShowCallout = true 

       //here We put a coordinates where we like to show bubble with text information up on the pin image 
       view.calloutOffset = CGPoint(x: -7, y: 7) 


       //Here this is a array of images 
       let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace] 

       //Here we set the resize of the image 
       let size = CGSize(width: 30, height: 30) 
       UIGraphicsBeginImageContext(size) 
       pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
       let resizeImage = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
       view.image = resizeImage 

       //Here we like to put into bubble window a singe for detail Informations 
       view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView 
       //Here we make change of standard pin image with our image 
       view.image = resizeImage 
      } 

      return view 
     } 
     return nil 
    } 
Các vấn đề liên quan