我想按照以下步骤添加自定义标记。我在谷歌地图上有用于标记的方法,但是我想按照以下方法将用户图像放在标记针上。 我有透明的标记图像,我想从URL加载用户图像。因此,图像必须动态。所以我不能放置静态标记。
//Add Marker
func dropMarker(_ location : CLLocation, _ isHotelLocation: Bool = false,_ memberModel : membersModel? = nil){
let marker = GMSMarker()
let position = self.checkIfMutlipleCoordinates(latitude: Float(location.coordinate.latitude), longitude: Float(location.coordinate.longitude))
marker.position = position
if isHotelLocation == false{
if let url = URL.init(string: memberModel!.avatar_url ?? ""){
let imageView = UIImageView(image: UIImage.init(named: "locationPin"))
imageView.sd_setImage(with: url, placeholderImage: UIImage.init(named: "locationPin"))
marker.iconView = imageView
}else{
marker.icon = UIImage(named : "LocationPlaceholderPin")
}
marker.userData = memberModel
}else{
marker.icon = UIImage(named : "hotelMarker")
hotelLocationMarker = marker
}
marker.map = mapView
arrMarkers.append(marker)
}
答案 0 :(得分:0)
使用此扩展程序从服务器下载图像:
extension UIImageView {
func downloaded(from url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) { // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloaded(from link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) { // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
guard let url = URL(string: link) else { return }
downloaded(from: url, contentMode: mode)
}
}
然后将图像作为子视图添加到标记图像:
var userImage = UIImageView()
userImage.downloaded(from: "user image url")
let userMarkerImage = UIImageView(named: "your marker placeholder")
userMarkerImage.addsubView(userImage)
然后将图像设置为标记:
marker.icon = userMarkerImage
注意
在添加为子视图之前,应先设置图像的框架