如何在Swift中创建具有组合图像的GMSMarker

3
我需要在iOS应用程序中的Google Maps屏幕上创建一个GMSMarker。我需要将标记设置为图像组合,即通用标记图像和用户图像的组合。 例如:Generic marker 在此标记内部,我需要适应用户的图像。我已经将这两个图像放在我的资源中。 我尝试过:
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
    }

但是这返回的图像不完美。 有其他解决方案吗? 注意:这里提供的示例标记图像并非我实际使用的那个。那是一个矩形标记。


您可能需要的内容在这里:https://developers.google.com/maps/documentation/ios-sdk/marker - Pushp
1个回答

3

这里我提供给你一些示例代码,请尝试它,它会正常工作并根据您的需求进行自定义/修改:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接