Man*_*ish 5 google-maps ios swift
我有一个 GMSMapView 类型的 Google 地图,并且该地图上显示了标记,并且我还在该 mapView 上实现了 Google 集群。
\n所以我真正想要实现的是我想点击集群图标和特定集群的图标更改。但目前这种情况没有发生,因为当我点击所选标记时,只有单个标记会发生变化,但集群项目不会发生变化。
\n我的集群组显示在地图上
\n\n我真正想要达到的目标
\n\n我在个人标记方面实际取得的成就。
\n\n因此,上面的图像是群集标记,我想在该群集项上渲染一个图标,但不幸的是,它 \xe2\x80\x99s 没有渲染为我无法在GMUClusterManagerDelegate和GMUClusterRendererDelegate的委托中访问的图像。\n我没有得到无论如何,如何在群集标记的点击操作上更改群集图标。
\n注意:- 我想更改群集项目的群集点击上的图标并且我已经实现了所选的单个标记
\n下面是我到目前为止所做的代码
\n override func viewDidLoad() {\n super.viewDidLoad()\n // Do any additional setup after loading the view.\n propertyMapView.delegate = self\n createListOfPOIItems()\n }\n\n // MARK : -- Map related functions\n//creating POOitems for the map\n func createListOfPOIItems() {\n self.propertyPOIItemList = []\n for index in 0..<self.propertyListModalArray.count {\n let propertylistmodal = self.propertyListModalArray[index]\n if UtilitySharedClass.sharedInstance.isvalidGeoLat(propertylistmodal.propertylat) && UtilitySharedClass.sharedInstance.isvalidGeoLong(propertylistmodal.propertyLong) {\n let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(Float(propertylistmodal.propertylat!)!), longitude: CLLocationDegrees(Float(propertylistmodal.propertyLong!)!))\n if universityLocation == coordinate{\n continue\n }\n let item = POIItem(position: coordinate, name: propertylistmodal.propertyName ?? "", propertyListModal: propertylistmodal)\n self.propertyPOIItemList?.append(item)\n }\n }\n initiateClustering()\n }\n\n func initiateClustering() {\n // Set up the cluster manager with the supplied icon generator and\n // renderer.\n let iconGenerator = ClusterIconGenerator()\n let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()\n let renderer = GMUDefaultClusterRenderer(mapView: propertyMapView,\n clusterIconGenerator: iconGenerator)\n clusterManager = GMUClusterManager(map: propertyMapView, algorithm: algorithm,\n renderer: renderer)\n renderer.delegate = self\n renderer.minimumClusterSize = 2\n \n //renderer.animationDuration = 0.2\n \n // Generate and add random items to the cluster manager.\n generateClusterItems()\n \n // Call cluster() after items have been added to perform the clustering\n // and rendering on map.\n clusterManager.cluster()\n \n clusterManager.setDelegate(self, mapDelegate: self)\n \n self.setUniversityLocation()\n }\n\nRun Code Online (Sandbox Code Playgroud)\n为地图添加集群
\n /// adds property location to the cluster manager.\n private func generateClusterItems() {\n guard let items = self.propertyPOIItemList else { return }\n items.forEach({ (item) in\n clusterManager?.add(item)\n clusterManager?.cluster()\n })\n }\nRun Code Online (Sandbox Code Playgroud)\n实现集群的代表
\nextension SearchViewController: GMSMapViewDelegate, GMUClusterManagerDelegate, GMUClusterRendererDelegate {\n \n func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? {\n let marker = CustomMarker()\n if let itemMarker = object as? GMUCluster {\n marker.markerType = .clusterType\n marker.markerCount = itemMarker.items.count\n } else if let itemMarker = object as? POIItem {\n marker.markerType = .itemType\n itemMarker.marker = marker\n marker.propertyListModal = itemMarker.propertyListModal\n }\n return marker\n }\n \n func renderer(_ renderer: GMUClusterRenderer, didRenderMarker marker: GMSMarker) {\n if let customMarker = marker as? CustomMarker {\n if customMarker.markerType == .itemType {\n let propertyMarker = createMarkerView(isSelected: customMarker.isSelected)\n propertyMarker.markerPriceLabel.text = customMarker.propertyListModal?.propertyPrice\n marker.icon = propertyMarker.asImage()\n marker.groundAnchor = CGPoint(x: 0.5, y: 1)\n }\n }\n }\n \n // MARK: - GMUClusterManagerDelegate\n func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool {\n let newCamera = GMSCameraPosition.camera(withTarget: cluster.position,\n zoom: propertyMapView.camera.zoom)\n let update = GMSCameraUpdate.setCamera(newCamera)\n propertyMapView.moveCamera(update)\n\n isClusterTapped = true\n\n clusterPropertyList = []\n let properties = cluster.items\n properties.forEach({ (property) in\n if let propertyModal = property as? POIItem, propertyModal.propertyListModal != nil{\n clusterPropertyList.append(propertyModal.propertyListModal!)\n }\n })\n \n if let previousMarker = propertyMapView.selectedMarker as? CustomMarker{\n previousMarker.isSelected = !previousMarker.isSelected\n let propertyMarker = createMarkerView(isSelected: previousMarker.isSelected)\n propertyMarker.markerPriceLabel.text = previousMarker.propertyListModal?.propertyPrice\n previousMarker.icon = propertyMarker.asImage()\n }\n self.propertyMapView.selectedMarker = nil\n\n self.propertyListContainerForMap.isHidden = false\n self.propertyViewForMapCollectionView.reloadData()\n return true\n }\n \n \n func clusterManager(_ clusterManager: GMUClusterManager, didTap clusterItem: GMUClusterItem) -> Bool {\n let newCamera = GMSCameraPosition.camera(withTarget: clusterItem.position,\n zoom: propertyMapView.camera.zoom)\n let update = GMSCameraUpdate.setCamera(newCamera)\n propertyMapView.moveCamera(update)\n \n if let previousMarker = propertyMapView.selectedMarker as? CustomMarker{\n previousMarker.isSelected = !previousMarker.isSelected\n let propertyMarker = createMarkerView(isSelected: previousMarker.isSelected)\n propertyMarker.markerPriceLabel.text = previousMarker.propertyListModal?.propertyPrice\n previousMarker.icon = propertyMarker.asImage()\n }\n \n let item = clusterItem as? POIItem\n if let tappedMarker = item?.marker as? CustomMarker{\n tappedMarker.isSelected = !tappedMarker.isSelected\n let propertyMarker = createMarkerView(isSelected: tappedMarker.isSelected)\n propertyMarker.markerPriceLabel.text = tappedMarker.propertyListModal?.propertyPrice\n tappedMarker.icon = propertyMarker.asImage()\n propertyMapView.selectedMarker = tappedMarker\n }\n \n isClusterTapped = false\n self.propertyListContainerForMap.isHidden = false\n self.propertyViewForMapCollectionView.reloadData()\n return true\n }\n}\n\nRun Code Online (Sandbox Code Playgroud)\n我的 POItem 类
\nimport Foundation\nimport GoogleMapsUtils\nimport GoogleMaps\n\nclass POIItem: NSObject, GMUClusterItem {\n var position: CLLocationCoordinate2D\n var name: String!\n var propertyListModal: PeropertyListModel?\n var marker: GMSMarker?\n \n init(position: CLLocationCoordinate2D, name: String, propertyListModal: PeropertyListModel) {\n self.position = position\n self.name = name\n self.propertyListModal = propertyListModal\n }\n}\n\nRun Code Online (Sandbox Code Playgroud)\nIconGenerator 类
\nimport Foundation\nimport GoogleMapsUtils\n\nclass ClusterIconGenerator: GMUDefaultClusterIconGenerator {\n \n override func icon(forSize size: UInt) -> UIImage {\n let image = textToImage(drawText: String(size) as NSString,\n inImage: UIImage(named: "clusterMarker")!,\n font: UIFont(name: "Futura-Medium", size: 12.0) ?? UIFont.systemFont(ofSize: 12))\n return image\n }\n \n private func textToImage(drawText text: NSString, inImage image: UIImage, font: UIFont) -> UIImage {\n \n UIGraphicsBeginImageContext(image.size)\n image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))\n \n let textStyle = NSMutableParagraphStyle()\n textStyle.alignment = NSTextAlignment.center\n let textColor = UIColor.black\n let attributes=[\n NSAttributedString.Key.font: font,\n NSAttributedString.Key.paragraphStyle: textStyle,\n NSAttributedString.Key.foregroundColor: textColor]\n \n // vertically center (depending on font)\n let textH = font.lineHeight\n let textY = (image.size.height-textH)/2\n let textRect = CGRect(x: 0, y: textY, width: image.size.width, height: textH)\n text.draw(in: textRect.integral, withAttributes: attributes)\n let result = UIGraphicsGetImageFromCurrentImageContext()\n UIGraphicsEndImageContext()\n return result!\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n上面是我到目前为止所做的代码,但不值得,请您帮我找到出路,如何实现在点击操作时在集群项目标记上渲染图标的解决方案。
\n
如果您有兴趣使用存储桶索引更改图标,我想您可以使用下面的代码来更改图像。您可以使用存储桶索引并分别显示簇标记的图标。
let iconGenerator = GMUDefaultClusterIconGenerator(buckets: [5, 10, 15], backgroundImages: [#imageLiteral(resourceName: "clusterSelectedMarker"), #imageLiteral(resourceName: "clusterSelectedMarker"), #imageLiteral(resourceName: "clusterSelectedMarker")])
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
clusterIconGenerator: iconGenerator)
var renderer = CustomClusterRenderer(mapView: propertyMapView, clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: propertyMapView, algorithm: algorithm,
renderer: renderer)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1293 次 |
| 最近记录: |