2012-03-22 33 views
17

Có thể thay thế biểu tượng ghim của chú thích bằng nhãn văn bản động không?Thay thế pin biểu tượng bằng nhãn văn bản trong chú thích?

Có thể sử dụng css hoặc tự động tạo hình ảnh?

Ví dụ: nhãn được thực hiện bằng CSS trên API Google Maps có JavaScript.

+0

Xin chào, hãy thử giải thích thêm một chút. Có thể một số mã của những gì bạn đã thử. –

Trả lời

30

Có, có thể.

Trong MapKit trên iOS, bạn cần triển khai phương thức ủy quyền viewForAnnotation và trả lại MKAnnotationView bằng cách thêm UILabel vào đó.

Ví dụ:

-(MKAnnotationView *)mapView:(MKMapView *)mapView 
    viewForAnnotation:(id<MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    static NSString *reuseId = @"reuseid"; 
    MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (av == nil) 
    { 
     av = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease]; 

     UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease]; 
     lbl.backgroundColor = [UIColor blackColor]; 
     lbl.textColor = [UIColor whiteColor]; 
     lbl.alpha = 0.5; 
     lbl.tag = 42; 
     [av addSubview:lbl]; 

     //Following lets the callout still work if you tap on the label... 
     av.canShowCallout = YES; 
     av.frame = lbl.frame; 
    } 
    else 
    { 
     av.annotation = annotation; 
    } 

    UILabel *lbl = (UILabel *)[av viewWithTag:42]; 
    lbl.text = annotation.title;   

    return av; 
} 

Hãy chắc chắn rằng tài sản delegate giao diện bản đồ được thiết lập bằng cách khác phương pháp đại biểu này sẽ không được gọi và bạn sẽ nhận được ghim màu đỏ mặc định để thay thế.

+0

Cảm ơn bạn rất nhiều vì đã trả lời nhanh. Tôi sẽ sớm kiểm tra mã này. Hẹn sớm gặp lại. – joumerlin

+0

Làm việc tốt, thx ... – joumerlin

2

Đây là biến thể Swift 3 của phương thức đại biểu được đề cập trong chú thích của Anna ở trên. Đảm bảo rằng lớp của bạn tuân theo MKMapViewDelegate và rằng ủy nhiệm của mapView được đặt thành self trong viewDidLoad().

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

    let reuseId = "reuseid" 
    var av = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
    if av == nil { 
     av = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30)) 
     lbl.backgroundColor = .black 
     lbl.textColor = .white 
     lbl.alpha = 0.5 
     lbl.tag = 42 
     av?.addSubview(lbl) 
     av?.canShowCallout = true 
     av?.frame = lbl.frame 
    } 
    else { 
     av?.annotation = annotation 
    } 

    let lbl = av?.viewWithTag(42) as! UILabel 
    lbl.text = annotation.title! 

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