2017-03-09 31 views
0

我需要在iOS應用程序中爲我的Google地圖屏幕創建一個GMSMarker。我需要標記爲圖像的組合,即通用標記圖像和用戶圖像。 例如:Generic marker如何在Swift中使用組合圖像創建GMSMarker

在這個標記中,我需要適合用戶的圖像。我的資產中都有圖片。 它試圖

func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! { 
     UIGraphicsBeginImageContext(size) 

     draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 

     image.draw(in: rect) 
     let result = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return result 
    } 

但這返回一個不完美的圖像。 有替代解決方案嗎? 注意:這裏提供的示例標記圖像不是我實際使用的圖像。這是一個矩形標記。

+0

可能你想在這裏:https://developers.google.com/地圖/文檔/ IOS-SDK /標記 – Pushp

回答

1

我在這裏爲您提供一些示例代碼,試試吧,它會工作&定製/修改根據您的要求:

let marker = GMSMarker() 
let lat = Double("13.063754") 
let long = Double("80.24358699999993") 
marker.position = CLLocationCoordinate2DMake(lat!,long!) 

///Creating UIView for Custom Marker 
let DynamicView=UIView(frame: CGRectMake(0, 0, 50, 50)) 
DynamicView.backgroundColor=UIColor.clearColor() 

//Creating Marker Pin imageview for Custom Marker 
var imageViewForPinMarker : UIImageView 
imageViewForPinMarker = UIImageView(frame:CGRectMake(0, 0, 40, 50)); 
imageViewForPinMarker.image = UIImage(named:"LocationPin") 

//Creating User Profile imageview 
var imageViewForUserProfile : UIImageView 
imageViewForUserProfile = UIImageView(frame:CGRectMake(0, 0, 35, 35)); 
imageViewForUserProfile.image = UIImage(named:"userprofile") 

//Adding userprofile imageview inside Marker Pin Imageview 
imageViewForPinMarker.addSubview(imageViewForUserProfile) 

//Adding Marker Pin Imageview isdie view for Custom Marker 
DynamicView.addSubview(imageViewForPinMarker) 

//Converting dynamic uiview to get the image/marker icon. 
UIGraphicsBeginImageContextWithOptions(DynamicView.frame.size, false, UIScreen.mainScreen().scale) 
DynamicView.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
let imageConverted: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

marker.icon = imageConverted 
marker.map = self.mapView