Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift |在自定义GMSMarker中显示图像_Ios_Swift_Google Maps - Fatal编程技术网

Ios Swift |在自定义GMSMarker中显示图像

Ios Swift |在自定义GMSMarker中显示图像,ios,swift,google-maps,Ios,Swift,Google Maps,我想在谷歌地图的自定义标记中显示用户的个人资料图片。像所有其他人一样,我在customMarker的init:中尝试了这段代码 self.location = location position = location icon = UIImage(named: "live location") groundAnchor = CGPoint(x: 0.5, y: 1) 是否有任何可能的方式在标记图标的圆圈中显示另一个图像。例如,使用xib。您可以使用以下两种方法: func drawImageW

我想在谷歌地图的自定义标记中显示用户的个人资料图片。像所有其他人一样,我在customMarker的
init:
中尝试了这段代码

self.location = location
position = location
icon = UIImage(named: "live location")
groundAnchor = CGPoint(x: 0.5, y: 1)

是否有任何可能的方式在标记图标的圆圈中显示另一个图像。例如,使用xib。

您可以使用以下两种方法:

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
以下是输出:


您可以在
nib
中以编程方式创建/设计自定义视图,然后将其分配给标记,如下所示

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

您可以通过创建一个自定义类
UIView
来实现这一点。GMSMarker没有属性
customView
,我们只有
iconView
。使用
iconView
没有帮助。您希望我在哪里使用
view.addSubview(imageView)
,因为
GMSMarker
不允许使用
addSubview
drawImageWithProfilePic(pp:#imageLiteral(resourceName:“no\u pic”),image:#imageLiteral(resourceName:“pin short”)
将返回图像。您只需将其设置在标记器的图标上。
    //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