我想在我的Google地图自定义标记中显示用户的个人资料图片。像其他所有人一样,我在customMarker的init:
中尝试了此代码
self.location = location
position = location
icon = UIImage(named: "live location")
groundAnchor = CGPoint(x: 0.5, y: 1)
有没有办法在标记图标的圆圈中显示其他图像。例如使用xib。
答案 0 :(得分:1)
您可以使用以下两种方法:
func drawImageWithProfilePic(pp: UIImage, image: UIImage) -> UIImage {
let imgView = UIImageView(image: image)
let picImgView = UIImageView(image: pp)
picImgView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
imgView.addSubview(picImgView)
picImgView.center.x = imgView.center.x
picImgView.center.y = imgView.center.y - 7
picImgView.layer.cornerRadius = picImgView.frame.width/2
picImgView.clipsToBounds = true
imgView.setNeedsLayout()
picImgView.setNeedsLayout()
let newImage = imageWithView(view: imgView)
return newImage
}
func imageWithView(view: UIView) -> UIImage {
var image: UIImage?
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
return image ?? UIImage()
}
这里pp
是您的个人资料照片,image
是照片图标。
您可以根据需要设置个人资料照片的框架。
我已经尝试过了:
修改
let marker = GMSMarker(position: coordinate)
marker.icon = drawImageWithProfilePic(pp: imgPP, image: img)
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = viewGoogleMap
这是输出:
答案 1 :(得分:0)
您可以在nib
或programmatically
中创建/设计自定义视图,然后将其分配给您的标记。
let someView = YourCustomView()
someView.markerImageView.image = UIImage(named: "markerImage")
someView.userImageView.image = UIImage(named: "userImage")
let marker = GMSMarker()
marker.map = yourMapView
marker.position = somePositionCoordinate;
marker.iconView = someView
答案 2 :(得分:0)
//put this code where you get image
let url = URL(string: "your_imageUrlString")
let data = try? Data(contentsOf: url!)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!)
marker.icon = self.drawImageWithProfilePic(pp: UIImage.init(data:data!)!, image: UIImage.init(named: "red")!)
marker.title = location
marker.snippet = name + " " + mobile
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = self.mapView
//put this code in your viewController class
func drawImageWithProfilePic(pp: UIImage, image: UIImage) -> UIImage {
let imgView = UIImageView(image: image)
imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let picImgView = UIImageView(image: pp)
picImgView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
imgView.addSubview(picImgView)
picImgView.center.x = imgView.center.x
picImgView.center.y = imgView.center.y - 7
picImgView.layer.cornerRadius = picImgView.frame.width/2
picImgView.clipsToBounds = true
imgView.setNeedsLayout()
picImgView.setNeedsLayout()
let newImage = imageWithView(view: imgView)
return newImage
}
func imageWithView(view: UIView) -> UIImage {
var image: UIImage?
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
return image ?? UIImage()
}
//here "red" is dummy background image