【问题标题】:How to fix the size and the color of my custom marker如何修复自定义标记的大小和颜色
【发布时间】:2018-02-02 09:55:08
【问题描述】:

我在我的 mapView(谷歌地图)中添加了一个自定义标记

func addMarker(place:EClass) {    
    DispatchQueue.main.async {
        guard let coordinates = place.location  else {
            return
        }
        self.destination = coordinates

       //Custom marker
       let markerImage = UIImage(named: "marker 2 copy.png")!.withRenderingMode(.alwaysTemplate)
       let markerView = UIImageView(image: markerImage)

       self.marker = GMSMarker()
       self.marker?.position = coordinates
       self.marker?.title = place.name
       self.marker?.map = self.mapView
       self.marker?.iconView = markerView
       self.mapView.selectedMarker = self.marker

       if self.currentLocation != nil {
           self.drawPath(startLocation: self.currentLocation!, endLocation: coordinates)
       }        
    }
}

但是当我运行应用程序时它的大小太大了,那么我该如何调整大小呢?我必须以编程方式执行此操作,还是必须更改我在项目中添加的 png 的大小?此外,当我运行应用程序时,我的 png 的颜色也不相同(即红色和黑色),我的自定义标记显示为白色。

【问题讨论】:

  • 如果你使用.alwaysTemplate,你应该为你的markerView设置一个tintColor

标签: ios swift google-maps swift4


【解决方案1】:

尝试使用GMSMarker.icon 而不是GMSMarker.iconView

let markerImage = UIImage(named: "marker 2 copy.png")!.withRenderingMode(.alwaysTemplate)
self.marker?.icon = self.image(markerImage, scaledToSize: CGSize(width: 20, height: 20))

//Image function
fileprivate func image(_ originalImage:UIImage, scaledToSize:CGSize) -> UIImage {
    if originalImage.size.equalTo(scaledToSize) {
        return originalImage
    } 
    UIGraphicsBeginImageContextWithOptions(scaledToSize, false, 0.0)
    originalImage.draw(in: CGRect(x: 0, y: 0, width: scaledToSize.width, height: scaledToSize.height))
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2014-12-15
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多