3

我在地圖上有幾個標記,它們分別代表一個具有不同主題的路徑。我希望用戶在選擇標記之前能夠看到每個主題,所以我打算在每個主題上添加一個簡單的文本標籤。這似乎不是ios的谷歌地圖中的嵌入式功能。有沒有辦法解決?如何在Google地圖上標註地圖標記ios

回答

22

設置一個UILabel,設置它,將其渲染到UIImage並將其設置爲標記的圖標。

//setup label 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]; 
label.text = @"test"; 
label.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5]; 

//grab it 
UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, [[UIScreen mainScreen] scale]); 
[label.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * icon = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//prepare options 
GMSMarkerOptions *options = [GMSMarkerOptions options]; 
options.position = CLLocationCoordinate2DMake(latitude, longitude); 
options.title = [NSString stringWithFormat:@"#%d", count_]; 
options.icon = icon; 
+0

未來 – 2013-04-04 19:52:29

+0

如果你想有一個標籤和銷示例代碼,使其既成爲一個圖像,你可以捕捉任何你喜歡的視圖 – 2013-04-04 19:59:47

+0

哇感謝您的完整答案,現在就試試。我想到了這一點,但我不知道renderInContext是一個選項 – CSStudent 2013-04-05 00:02:52

2

我是一個初學者迅速,但我可以轉換Daij-Djan的回答是:

let label = UILabel() 
label.frame = CGRect(x:0, y:0, width: 50, height: 20) 
label.text = "test" 
label.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5) 
label.textColor = UIColor.whiteColor() 
label.adjustsFontSizeToFitWidth = true 
UIGraphicsBeginImageContextWithOptions(label.frame.size, false, UIScreen.mainScreen().scale) 
if let currentContext = UIGraphicsGetCurrentContext() 
{ 
    label.layer.renderInContext(currentContext) 
    let imageMarker = UIImage() 
    imageMarker = UIGraphicsGetImageFromCurrentImageContext() 
    let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, long)) 
    marker.icon = imageMarker 
    marker.map = mapView 
} 
UIGraphicsEndImageContext()