Здесь показаны пошаговые инструкции о том, как сгруппировать несколько маркеров карты с помощью кластеризации маркеров
1:
class HFDashBordVC: UIViewController,GMUClusterManagerDelegate,GMSMapViewDelegate{
private var clusterManager: GMUClusterManager!
var cameraPosition = GMSCameraPosition()
var markerPin = GMSMarker()
var currentLocationCircle = GMSCircle()
var locationManager = CLLocationManager()
var currentLocation: CLLocation?
var zoomLevel: Float = 18.0
@IBOutlet var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
setupClustering()
}
func setupClustering() {
let iconGenerator = CustomClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = CustomRendererMarkers(mapView: mapView,
clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm,
renderer: renderer)
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
}
private func setClusterParkingPin() {
clusterManager.clearItems()
for object in AppUtilites.sharedInstance.safeParkingArray {
let model = object as! AMParkModel
let lat = Double(model.latitude)!
let lng = Double(model.longitude)!
let position = CLLocationCoordinate2DMake(lat, lng)
let marker = GMSMarker(position: position)
marker.icon = HFAsset.ic_p_pin_orange.image
let userData = model
let item = POIItem(position: position, marker: marker, userData: userData)
self.clusterManager.add(item)
self.setCurrentPin(zoomLevel: 14.0)
}
}
2 :
class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var userData: AnyObject!
@objc var marker: GMSMarker!
init(position: CLLocationCoordinate2D, marker: GMSMarker, userData: AnyObject) {
self.position = position
self.marker = marker
self.userData = userData
}
}
3 :
class CustomRendererMarkers: GMUDefaultClusterRenderer {
var mapView:GMSMapView?
let kGMUAnimationDuration: Double = 0.5
override init(mapView: GMSMapView, clusterIconGenerator iconGenerator: GMUClusterIconGenerator) {
super.init(mapView: mapView, clusterIconGenerator: iconGenerator)
}
func markerWithPosition(position: CLLocationCoordinate2D, from: CLLocationCoordinate2D, userData: AnyObject, clusterIcon: UIImage, animated: Bool) -> GMSMarker {
let initialPosition = animated ? from : position
let marker = GMSMarker(position: initialPosition)
marker.userData! = userData
if clusterIcon.cgImage != nil {
marker.icon = clusterIcon
}
else {
marker.icon = self.getCustomTitleItem(userData: userData)
}
marker.map = mapView
if animated
{
CATransaction.begin()
CAAnimation.init().duration = kGMUAnimationDuration
marker.layer.latitude = position.latitude
marker.layer.longitude = position.longitude
CATransaction.commit()
}
return marker
}
func getCustomTitleItem(userData: AnyObject) -> UIImage {
let item = userData as! POIItem
return item.marker.icon!
}
}
4 :
class CustomClusterIconGenerator: GMUDefaultClusterIconGenerator {
override func icon(forSize size: UInt) -> UIImage {
let image = textToImage(drawText: (String(size) as NSString) as String,
inImage: HFAsset.ic_parking_clustering.image,
font: UIFont.init(name: "Montserrat-Medium", size: 12.0)!)
return image
}
private func textToImage(drawText text: String, inImage image: UIImage, font: UIFont) -> UIImage {
var pinCount = text
if Int(text)! > 9999 {
pinCount = "+9999"
}
UIGraphicsBeginImageContext(image.size)
image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
let textStyle = NSMutableParagraphStyle()
textStyle.alignment = NSTextAlignment.center
let textColor = UIColor.white
let attributes=[
NSAttributedStringKey.font: font,
NSAttributedStringKey.paragraphStyle: textStyle,
NSAttributedStringKey.foregroundColor: textColor,
NSAttributedStringKey.backgroundColor: appThemeColor]
// vertically center (depending on font)
let textH = font.lineHeight
let textY = (image.size.height-textH)/3
let textRect = CGRect(x: 0, y: textY, width: image.size.width, height: textH)
pinCount.draw(in: textRect.integral, withAttributes: attributes)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result!
}
}
5 :
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if markerPin == marker {
return true
}
if ((marker.userData as? GMUStaticCluster) != nil) { // Cluster Pin tap
UIView.animate(withDuration: 0.5, delay: 0.2, options: [.curveEaseOut],
animations: {
let newCamera = GMSCameraPosition.camera(withTarget: marker.position,
zoom: self.mapView.camera.zoom + 0.8)
let update = GMSCameraUpdate.setCamera(newCamera)
self.mapView.animate(with: update)
}, completion: {
finished in
})
}
if let poiItem = marker.userData as? POIItem { // Cluster Parking Pin tap
let Detail = self.storyboard?.instantiateViewController(withIdentifier: "HFParkingFullDetailsVC")as! HFParkingFullDetailsVC
Detail.parkModel = (poiItem.userData as? AMParkModel)!
self.navigationController?.pushViewController(Detail, animated: true)
}
person
Paresh Mangukiya
schedule
16.05.2019