2014-06-30 16 views

Trả lời

31

Bạn phải tạo chế độ xem, nơi bạn phải tạo imageView (với hình ảnh điểm đánh dấu) và Nhãn (kèm theo văn bản) và chụp ảnh màn hình của chế độ xem đó và đặt làm biểu tượng cho GMSMarker của bạn. Something như thế này:

- (void)foo 
{ 
    GMSMarker *marker = [GMSMarker new]; 

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,60,60)]; 
    UIImageView *pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myPin"]]; 
    UILabel *label = [UILabel new]; 
    label.text = @"1"; 
    //label.font = ...; 
    [label sizeToFit]; 

    [view addSubview:pinImageView]; 
    [view addSubview:label]; 
    //i.e. customize view to get what you need  


    UIImage *markerIcon = [self imageFromView:view]; 
    marker.icon = markerIcon;   
    marker.map = self.mapView;  
} 

- (UIImage *)imageFromView:(UIView *) view 
{ 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]); 
    } else { 
     UIGraphicsBeginImageContext(view.frame.size); 
    } 
    [view.layer renderInContext: UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 
+0

Xin chào tôi đã làm theo quy trình tương tự, nhưng vấn đề là hình ảnh của tôi không được hiển thị. – Diksha

10

enter image description here

Nếu bạn muốn hiển thị một cái gì đó như thế này, sau đó chỉ cần làm theo các bước sau. Nó rất đơn giản, Bạn có thể sử dụng phương pháp này.

-(UIImage *)createImage:(NSUInteger)count{ //count is the integer that has to be shown on the marker 


UIColor *color = [UIColor redColor]; // select needed color 
NSString *string = [NSString stringWithFormat:@"%lu",(unsigned long)count]; // the string to colorize 
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color }; 
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:string attributes:attrs]; // add Font according to your need 

UIImage *image = [UIImage imageNamed:@"ic_marker_orange"]; // The image on which text has to be added 
UIGraphicsBeginImageContext(image.size); 
[image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)]; 

CGRect rect = CGRectMake(20,5, image.size.width, image.size.height);// change the frame of your text from here 
[[UIColor whiteColor] set]; 
[attrStr drawInRect:rect]; 
UIImage *markerImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return markerImage;} 

và khi bạn thiết lập đánh dấu vào bản đồ thì chỉ cần đặt

GMSMarker *marker = [[GMSMarker alloc] init]; 
    marker.icon = [self createImage:[model.strFriendCount integerValue]]; // pass any integer to the method. 
1

Dưới đây là phiên bản nhanh chóng của Kunal's answer:

func createImage(_ count: Int) -> UIImage { 
    //count is the integer that has to be shown on the marker 
    let color = UIColor.red 
    // select needed color 
    let string = "\(UInt(count))" 
    // the string to colorize 
    let attrs = [NSForegroundColorAttributeName: color] 
    let attrStr = NSAttributedString(string: string, attributes: attrs) 
    // add Font according to your need 
    let image = UIImage(named: "ic_marker_orange")! 
    // The image on which text has to be added 
    UIGraphicsBeginImageContext(image.size) 
    image.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(image.size.width), height: CGFloat(image.size.height))) 
    let rect = CGRect(x: CGFloat(20), y: CGFloat(5), width: CGFloat(image.size.width), height: CGFloat(image.size.height)) 

    attrStr.draw(in: rect) 

    let markerImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return markerImage 
} 

Hy vọng điều này sẽ giúp người khác.

+0

Đã xảy ra sự cố khi đánh dấu là một nhóm, nhưng không quá nhiều (ví dụ: 100) trong đó bộ nhớ là một vấn đề: https://stackoverflow.com/questions/43910085/googlemaps-sdk-for-ios-swift-3-when -hiding-a-marker-và-sau đó-thêm-the-map-v – Efren

Các vấn đề liên quan