gpt4 book ai didi

ios - 你如何改变谷歌地图的集群图标?

转载 作者:行者123 更新时间:2023-12-03 20:46:29 28 4
gpt4 key购买 nike

我有一个 GMSMapView 类型的 Google map ,并且我在该 map 上显示了标记,并且我还为该 mapView 实现了 Google 集群。
所以我真正想要实现的是我想点击特定集群的集群图标和图标更改。但是目前这并没有发生,因为当我点击选定的标记时,只有单个标记会发生变化,而不是集群 Item 会发生变化。
map 上显示的我的集群组
My CLuster Map Image Link
我真正想要达到的目标
What I want to achieve On Cluster Tap Image Link
我为个人标记实际取得的成就。
what I have achieved for individual marker Image Link
所以上面的图像是集群标记我想在该集群项目上呈现一个图标但不幸的是它没有呈现,因为我无法在 的代表中访问图像GMUClusterManagerDelegate GMUClusterRendererDelegate .
我不知道如何在集群标记的点击操作上更改集群图标。
注意:- 我想更改集群项目的集群点击图标并且我已经为选定的单个标记 实现了
下面是我到目前为止所做的代码

   override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
propertyMapView.delegate = self
createListOfPOIItems()
}

// MARK : -- Map related functions
//creating POOitems for the map
func createListOfPOIItems() {
self.propertyPOIItemList = []
for index in 0..<self.propertyListModalArray.count {
let propertylistmodal = self.propertyListModalArray[index]
if UtilitySharedClass.sharedInstance.isvalidGeoLat(propertylistmodal.propertylat) && UtilitySharedClass.sharedInstance.isvalidGeoLong(propertylistmodal.propertyLong) {
let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(Float(propertylistmodal.propertylat!)!), longitude: CLLocationDegrees(Float(propertylistmodal.propertyLong!)!))
if universityLocation == coordinate{
continue
}
let item = POIItem(position: coordinate, name: propertylistmodal.propertyName ?? "", propertyListModal: propertylistmodal)
self.propertyPOIItemList?.append(item)
}
}
initiateClustering()
}

func initiateClustering() {
// Set up the cluster manager with the supplied icon generator and
// renderer.
let iconGenerator = ClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: propertyMapView,
clusterIconGenerator: iconGenerator)
clusterManager = GMUClusterManager(map: propertyMapView, algorithm: algorithm,
renderer: renderer)
renderer.delegate = self
renderer.minimumClusterSize = 2

//renderer.animationDuration = 0.2

// Generate and add random items to the cluster manager.
generateClusterItems()

// Call cluster() after items have been added to perform the clustering
// and rendering on map.
clusterManager.cluster()

clusterManager.setDelegate(self, mapDelegate: self)

self.setUniversityLocation()
}

为 map 添加集群
    /// adds property location to the cluster manager.
private func generateClusterItems() {
guard let items = self.propertyPOIItemList else { return }
items.forEach({ (item) in
clusterManager?.add(item)
clusterManager?.cluster()
})
}
为集群实现委托(delegate)
extension SearchViewController:  GMSMapViewDelegate, GMUClusterManagerDelegate, GMUClusterRendererDelegate {

func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? {
let marker = CustomMarker()
if let itemMarker = object as? GMUCluster {
marker.markerType = .clusterType
marker.markerCount = itemMarker.items.count
} else if let itemMarker = object as? POIItem {
marker.markerType = .itemType
itemMarker.marker = marker
marker.propertyListModal = itemMarker.propertyListModal
}
return marker
}

func renderer(_ renderer: GMUClusterRenderer, didRenderMarker marker: GMSMarker) {
if let customMarker = marker as? CustomMarker {
if customMarker.markerType == .itemType {
let propertyMarker = createMarkerView(isSelected: customMarker.isSelected)
propertyMarker.markerPriceLabel.text = customMarker.propertyListModal?.propertyPrice
marker.icon = propertyMarker.asImage()
marker.groundAnchor = CGPoint(x: 0.5, y: 1)
}
}
}

// MARK: - GMUClusterManagerDelegate
func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool {
let newCamera = GMSCameraPosition.camera(withTarget: cluster.position,
zoom: propertyMapView.camera.zoom)
let update = GMSCameraUpdate.setCamera(newCamera)
propertyMapView.moveCamera(update)

isClusterTapped = true

clusterPropertyList = []
let properties = cluster.items
properties.forEach({ (property) in
if let propertyModal = property as? POIItem, propertyModal.propertyListModal != nil{
clusterPropertyList.append(propertyModal.propertyListModal!)
}
})

if let previousMarker = propertyMapView.selectedMarker as? CustomMarker{
previousMarker.isSelected = !previousMarker.isSelected
let propertyMarker = createMarkerView(isSelected: previousMarker.isSelected)
propertyMarker.markerPriceLabel.text = previousMarker.propertyListModal?.propertyPrice
previousMarker.icon = propertyMarker.asImage()
}
self.propertyMapView.selectedMarker = nil

self.propertyListContainerForMap.isHidden = false
self.propertyViewForMapCollectionView.reloadData()
return true
}


func clusterManager(_ clusterManager: GMUClusterManager, didTap clusterItem: GMUClusterItem) -> Bool {
let newCamera = GMSCameraPosition.camera(withTarget: clusterItem.position,
zoom: propertyMapView.camera.zoom)
let update = GMSCameraUpdate.setCamera(newCamera)
propertyMapView.moveCamera(update)

if let previousMarker = propertyMapView.selectedMarker as? CustomMarker{
previousMarker.isSelected = !previousMarker.isSelected
let propertyMarker = createMarkerView(isSelected: previousMarker.isSelected)
propertyMarker.markerPriceLabel.text = previousMarker.propertyListModal?.propertyPrice
previousMarker.icon = propertyMarker.asImage()
}

let item = clusterItem as? POIItem
if let tappedMarker = item?.marker as? CustomMarker{
tappedMarker.isSelected = !tappedMarker.isSelected
let propertyMarker = createMarkerView(isSelected: tappedMarker.isSelected)
propertyMarker.markerPriceLabel.text = tappedMarker.propertyListModal?.propertyPrice
tappedMarker.icon = propertyMarker.asImage()
propertyMapView.selectedMarker = tappedMarker
}

isClusterTapped = false
self.propertyListContainerForMap.isHidden = false
self.propertyViewForMapCollectionView.reloadData()
return true
}
}

我的 POItem 类
import Foundation
import GoogleMapsUtils
import GoogleMaps

class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String!
var propertyListModal: PeropertyListModel?
var marker: GMSMarker?

init(position: CLLocationCoordinate2D, name: String, propertyListModal: PeropertyListModel) {
self.position = position
self.name = name
self.propertyListModal = propertyListModal
}
}

IconGenerator 的类
import Foundation
import GoogleMapsUtils

class ClusterIconGenerator: GMUDefaultClusterIconGenerator {

override func icon(forSize size: UInt) -> UIImage {
let image = textToImage(drawText: String(size) as NSString,
inImage: UIImage(named: "clusterMarker")!,
font: UIFont(name: "Futura-Medium", size: 12.0) ?? UIFont.systemFont(ofSize: 12))
return image
}

private func textToImage(drawText text: NSString, inImage image: UIImage, font: UIFont) -> UIImage {

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.black
let attributes=[
NSAttributedString.Key.font: font,
NSAttributedString.Key.paragraphStyle: textStyle,
NSAttributedString.Key.foregroundColor: textColor]

// vertically center (depending on font)
let textH = font.lineHeight
let textY = (image.size.height-textH)/2
let textRect = CGRect(x: 0, y: textY, width: image.size.width, height: textH)
text.draw(in: textRect.integral, withAttributes: attributes)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result!
}
}

The above is the code I did so far but not worth please can you find me the way out how can I achieve the solution of rendering an icon on cluster Item marker on tap action.

最佳答案

如果您有兴趣使用存储桶索引更改图标
我想要更改图像,您可以使用下面的代码。您可以使用 buckets 索引并分别显示集群标记的图标。

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)

关于ios - 你如何改变谷歌地图的集群图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65370121/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com