func customMarkerView(titleText : String, markerImg : UIImage) -> UIImage{ let mainView : UIView = UIView() let subView : UIView = UIView() let padding : CGFloat = 8.0 let rightImgView : UIImageView = UIImageView() let markerImgView : UIImageView = UIImageView() let cfont = UIFont(name: "Roboto-Regular", size: 16.0) //createing marker label. let titleLabel : UILabel = UILabel(frame: CGRect(x: 4.0, y: 4.0, width: 150.0, height: cfont!.lineHeight)) titleLabel.lineBreakMode = .byWordWrapping titleLabel.font = cfont! titleLabel.text = titleText titleLabel.numberOfLines = 0 titleLabel.sizeToFit() titleLabel.backgroundColor = UIColor.white let noLines = titleLabel.frame.size.height / cfont!.lineHeight if noLines > 2{ titleLabel.numberOfLines = 2 titleLabel.frame = CGRect(x: 4.0, y: 4.0, width: titleLabel.frame.width, height: 2 * cfont!.lineHeight) }else{ titleLabel.frame = CGRect(x: 4.0, y: 4.0, width: titleLabel.frame.width, height: 24.0) } let rightArrow = UIImage(named: "right-arrow") rightImgView.frame = CGRect(x: titleLabel.frame.origin.x + titleLabel.frame.width + padding, y: 4.0, width: 24.0, height: titleLabel.frame.height) rightImgView.image = rightArrow! rightImgView.contentMode = .center subView.addSubview(titleLabel) subView.addSubview(rightImgView) subView.frame = CGRect(x: 0.0, y: 0.0, width: 2 * 4.0 + titleLabel.frame.width + padding + rightImgView.frame.width, height: 2 * 4.0 + titleLabel.frame.height) subView.backgroundColor = UIColor.white markerImgView.frame = CGRect(x: subView.center.x - markerImg.size.width/2, y: subView.frame.height + 4.0, width: markerImg.size.width, height: markerImg.size.height) markerImgView.image = markerImg markerImgView.contentMode = .scaleAspectFit mainView.addSubview(markerImgView) mainView.frame = CGRect(x: 0.0, y: 0.0, width: subView.frame.width, height: subView.frame.height + 4.0 + markerImgView.frame.height) mainView.addSubview(subView) mainView.backgroundColor = UIColor.clear UIGraphicsBeginImageContextWithOptions(mainView.bounds.size, false, UIScreen.main.scale) mainView.layer.render(in: UIGraphicsGetCurrentContext()!) let icon : UIImage = UIGraphicsGetImageFromCurrentImageContext()! UIGraphicsEndImageContext() return icon }
你可以加两个 MarkerViews 在地图中使用 GMSMarker()
MarkerViews
GMSMarker()
把它们放进去 array 然后你可以使用 GMSCoordinateBounds 聚焦地图以显示所有标记。 下面的代码显示了如何聚焦地图。
array
GMSCoordinateBounds
var bounds = GMSCoordinateBounds() for marker in yourArrayOfMarkers { bounds = bounds.includingCoordinate(marker.position) } let update = GMSCameraUpdate.fit(bounds, withPadding: 60) mapView.animate(with: update)