谷歌地图在使用自定义颜色设置图标属性绘制了1300多个标记后崩溃



我可以绘制8000多个默认的红色标记,而无需为标记设置图标属性。但我想根据标记的值绘制不同颜色的标记。在XCOde中,我收到以下警告:-

((null)) was false: Reached the max number of texture atlases, can not allocate more.
((null)) was false: Failed to get the icon for the given CGImageRef.
((null)) was false: Failed to allocate texture space for marker

来自Google Map SDK&在那之后,大约1300个标记被撞毁。有没有其他方法可以在不崩溃1300多个标记的情况下为标记设置不同的颜色。

我正在设置标记的颜色如下:-

marker.icon = GMSMarker.markerImage(with: self.getColorsFromString(strColor: strColor))
func getColorsFromString(strColor:String) -> UIColor
{
var color = UIColor()

switch strColor {
case "GREEN":
color = UIColor.green
case "YELLOW":
color = UIColor.yellow
case "RED":
color = UIColor.red
case "ORANGE":
color = UIColor.orange
case "BLUE":
color = UIColor.blue
case "CYAN":
color = UIColor.cyan
case "MAGENTA":
color = UIColor.magenta

default:
color = UIColor.red
print("default color")
}
return color

}

如何使用图像的结构?我们不需要调用GMSMarker.markerImage((数千次。

struct MarkerImage {
static let green = GMSMarker.markerImage(with: self.getColorsFromString(strColor: "GREEN"))
static let yellow = GMSMarker.markerImage(with: self.getColorsFromString(strColor: "YELLOW"))
static let red = GMSMarker.markerImage(with: self.getColorsFromString(strColor: "RED"))
}
func getIcon(color: String) -> Image {
switch(color) {
case "GREEN": return MarkerImage.green
case "YELLOW": return MarkerImage.yellow
default: return MarkerImage.red
}
}
marker.icon = getIcon(color)

若它有效,我们可以创建一个颜色名称的枚举,并对其进行扩展以返回图像。

感谢winner.ktw的这个解决方案。这个解决方案没有直接起作用,但我做了一些更改,然后它对我起了作用。因为你的解决方案,我有了一个主意。再次感谢!我在这里分享我编辑过的winner.ktw代码版本。

struct MarkerImage{
static var shared = MarkerImage()
lazy var  green = GMSMarker.markerImage(with: self.getColorsFromString(strColor: "GREEN"))
lazy var  yellow = GMSMarker.markerImage(with: self.getColorsFromString(strColor: "YELLOW"))
lazy var  red = GMSMarker.markerImage(with: self.getColorsFromString(strColor: "RED"))
func getColorsFromString(strColor:String) -> UIColor {
var color = UIColor()

switch strColor {
case "GREEN":
color = UIColor.green
case "YELLOW":
color = UIColor.yellow
default:
color = UIColor.red
}
return color
}
}
func getIcon(color: String) -> UImage {
switch(color) {
case "GREEN": return MarkerImage.shared.green
case "YELLOW": return MarkerImage.shared.yellow
default: return MarkerImage.shared.red
}
}

最新更新