【问题标题】:How to create GMSMarker with combined images in Swift如何在 Swift 中使用组合图像创建 GMSMarker
【发布时间】:2017-03-09 15:16:03
【问题描述】:

我需要在 iOS 应用中为我的 Google 地图屏幕创建一个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
    }

但这返回了一个不完美的图像。 有替代解决方案吗? 注意:此处提供的示例标记图像不是我实际使用的。那是一个矩形标记。

【问题讨论】:

标签: ios swift google-maps


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多