问题
I have load multiple pins in GoogleMap with clustering and below is my code.
Link : https://github.com/googlemaps/google-maps-ios-utils/issues/235
func initializeClusterItems() {
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
renderer.delegate = self
clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
}
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
marker.groundAnchor = CGPoint(x: 0.1, y: 0.45)
if let markerData = (marker.userData as? POIItem) {
let infoWindow = Bundle.main.loadNibNamed("InitialMapInfoView", owner: self, options: nil)?.first as! InitialMapInfoView
infoWindow.imgUser.sd_setImage(with: URL(string: markerData.friend.user_details.user_photo_small), placeholderImage: #imageLiteral(resourceName: "User_profile"), options: .highPriority, completed: nil)
if let objCurrentMarker = SharedData.sharedInstance.allFriends.first(where: {$0.user_details.user_id == markerData.friend.user_details.user_id}) {
print("Latitude: \(markerData.position.latitude)")
print("Longitude: \(markerData.position.longitude)")
print(objCurrentMarker.user_details.screen_name)
print("Update Latitude: \(objCurrentMarker.user_details.latitude)")
print("Update Longitude: \(objCurrentMarker.user_details.longitude)")
markerData.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)
markerData.friend.user_details.isUserOnline = objCurrentMarker.user_details.isUserOnline
}
if !markerData.friend.user_details.isUserOnline {
infoWindow.imgCar.image = UIImage.init(named: "small_inactive_" + markerData.friend.user_details.car_personality_name)
}
else {
infoWindow.imgCar.image = UIImage.init(named: "small_" + markerData.friend.user_details.car_personality_name)
}
infoWindow.lblName.text = markerData.friend.user_details.name
infoWindow.btnImgVW.tag = markerData.userIndex
infoWindow.btnImgVW.addTarget(self, action: #selector(btnUserTapped(_:)), for: .touchUpInside)
marker.accessibilityHint = String(markerData.userIndex)
marker.iconView = infoWindow
marker.tracksViewChanges = false
}
}
func setMarkers() {
arrMarkers.removeAll()
for i in 0..<SharedData.sharedInstance.allFriends.count {
let marker = MyMarker()
let friend = SharedData.sharedInstance.allFriends[i]
marker.id = friend.user_details.user_id
marker.position = CLLocationCoordinate2D.init(latitude: friend.user_details.latitude , longitude: friend.user_details.longitude)
marker.accessibilityHint = String(i)
marker.icon = #imageLiteral(resourceName: "trans")
marker.tracksViewChanges = true
marker.map = mapView
arrMarkers.append(marker)
self.generatePOIItems(String(format: "%d", i), position: marker.position, icon: nil, friend: friend, userIndex: i)
}
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
}
func updateMarkers() {
clusterManager.cluster()
}
func generatePOIItems(_ accessibilityLabel: String, position: CLLocationCoordinate2D, icon: UIImage?, friend: WallBeeppClass, userIndex: Int) {
let name = "Item \(accessibilityLabel)"
let item = POIItem(position: CLLocationCoordinate2DMake(position.latitude, position.longitude), name: name, friend: friend, userIndex: userIndex)
clusterManager.add(item)
}
class POIItem: NSObject, GMUClusterItem {
var position: CLLocationCoordinate2D
var name: String!
var friend: WallBeeppClass!
var userIndex: Int!
init(position: CLLocationCoordinate2D, name: String, friend: WallBeeppClass, userIndex: Int) {
self.position = position
self.name = name
self.friend = friend
self.userIndex = userIndex
}
}
I want to update the users latitude & longitude inside clusterManagerItems.
I tried the below line to change location data inside func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker){}
but app crashing with this error
'All items should be mapped to a distance'
markerData.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)
If I add below line then it works but when I zoomIn/ZoomOut the map. clusterManager item load at old position and then it load to new position.
marker.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)
A you can see, I printed markerData.friend.user_details.latitude
details which contains old coordinats of user and I also printed user's latest data objCurrentMarker.user_details.latitude
which contains the latest coordinates.
EDIT1
I'm able to update the already added GMUClusterItem
but the issue is that when i call clusterManager.cluster()
again when I receive new data from server then GMUClusterItem
is gone when I zoomin the map and come again on map when zoomout the Map.
EDIT 2
Reference Video Link
https://gofile.io/?c=8z0vE5
EDIT 3
Custom Marker disappear because of the below code. when I comment below code then marker wasn't disappear from map.
CATransaction.begin()
CATransaction.setAnimationDuration(1.0)
marker.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)
CATransaction.commit()
Please guide me how to update already added Clustermanager item?
Thanks in advance
回答1:
One thing that worked for me was to disable animations at the renderer level, i.e., GMUClusterRenderer.animatesClusters = false
来源:https://stackoverflow.com/questions/57231345/custom-markers-disappear-on-zoomin-the-map-and-appear-on-zoomout-the-map-with-cl