swift ios中的自定义谷歌地图标记图标图像

Posted

技术标签:

【中文标题】swift ios中的自定义谷歌地图标记图标图像【英文标题】:custom google map marker icon image in swift ios 【发布时间】:2016-06-24 12:03:52 【问题描述】:

我正在将自定义视图加载从 xib 转换为 UIImage 以显示为标记图标。它已完成,但问题是在 for 循环中执行此操作以显示 100 个标记时 UI 卡住是因为加载 xib 自定义查看?

下面是for循环中调用的函数

func generateSpot(Prop: Property) -> Spot 

    let latStr = Prop.propLat
    let langStr = Prop.propLang as NSString
    let langDbl : Double = Double(langStr.floatValue)
    let latDbl : Double  = Double(latStr.floatValue)

    let marker:GMSMarker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(langDbl,latDbl)
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.title = Prop.propBuildingName as String
    marker.snippet = Prop.propCode as String
    marker.infoWindowAnchor = CGPointMake(0.44, 0.45)

    let sizeVar:CGSize = CGSize(width: 41, height: 37)

    let customeMarker :CustomMarker = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self.view, options: nil).first! as! CustomMarker

    customeMarker.frame = CGRectMake(customeMarker.frame.minX,customeMarker.frame.minY,customeMarker.frame.width,customeMarker.frame.height)
    customeMarker.mappedNumbers.layer.masksToBounds = true
    customeMarker.mappedNumbers.layer.cornerRadius = 8
    customeMarker.mappedNumbers.text = Prop.propSequence as String

    if Prop.propStatus == self.constants!.status.STATUS_COMPLETED_XML
    
        customeMarker.mappedNumbers.backgroundColor = self.utility!.uicolorFromHex(0x70b420)
        customeMarker.arrowImage.tintColor = self.utility!.uicolorFromHex(0x70b420)
    

    if Prop.propStatus == self.constants!.status.STATUS_DRAFT_XML
    
        customeMarker.mappedNumbers.backgroundColor = self.utility!.uicolorFromHex(0xd2cb46)
        customeMarker.arrowImage.tintColor = self.utility!.uicolorFromHex(0x70b420)


    
    if Prop.propStatus == self.constants!.status.STATUS_PENDING_XML
    
        customeMarker.mappedNumbers.backgroundColor = self.utility!.uicolorFromHex(0xd44646)
        customeMarker.arrowImage.tintColor = self.utility!.uicolorFromHex(0xd44646)


    


    UIGraphicsBeginImageContextWithOptions(sizeVar, false, 0)
    customeMarker.drawViewHierarchyInRect(customeMarker.bounds, afterScreenUpdates: true)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    marker.icon = newImage
    marker.map = self.googleMapView
    let spotObje:Spot = Spot()
    spotObje.location = marker.position
    spotObje.marker = marker
    return spotObje

【问题讨论】:

在后台线程中执行 在后台线程中尝试但出现错误 此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。 请在单独的gist.github.com 链接中显示更新的代码 gist.github.com/nitisha1/0763585550ed6be1cfa931408232f180 此代码是否在 bg 线程上加载内容?在我看来,一切都在主线程上完成? 【参考方案1】:

解决了 替换

 customeMarker.drawViewHierarchyInRect(customeMarker.bounds, afterScreenUpdates: true)

customeMarker.layer.renderInContext(UIGraphicsGetCurrentContext()!)

参考:drawViewHierarchyInRect:afterScreenUpdates: delays other animations

【讨论】:

以上是关于swift ios中的自定义谷歌地图标记图标图像的主要内容,如果未能解决你的问题,请参考以下文章

使用集群自定义谷歌地图上的标记

为当前位置自定义谷歌地图蓝点

如何上传图片,然后在 iOS 中基于该图片执行自定义谷歌图片搜索?

如何自定义谷歌地图以接收特定位置的通知

如何自定义谷歌课堂分享按钮的外观

谷歌地图每次都无法在地图上放置一些标记