本文介绍了自定义标记在放大地图时消失,并在通过聚类缩小地图时出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在具有集群功能的GoogleMap中加载了多个图钉,下面是我的代码.

I have load multiple pins in GoogleMap with clustering and below is my code.

链接: 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
    }
}

我想更新用户的纬度& clusterManagerItems内部的经度.

I want to update the users latitude & longitude inside clusterManagerItems.

我尝试了以下行来更改func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker){}内部的位置数据,但应用因此错误而崩溃

I tried the below line to change location data inside func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker){} but app crashing with this error

markerData.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)

如果我在行下方添加内容,则可以使用,但是当我放大/缩小地图时.将clusterManager项加载到旧位置,然后再加载到新位置.

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)

您可以看到,我打印了markerData.friend.user_details.latitude详细信息,其中包含用户的旧坐标,并且还打印了用户的最新数据objCurrentMarker.user_details.latitude,其中包含最新的坐标.

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.

我能够更新已经添加的GMUClusterItem,但是问题是,当我从服务器接收到新数据时再次调用clusterManager.cluster()时,当我放大地图并再次出现在地图上时,GMUClusterItem不见了缩小地图时.

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.

参考视频链接

https://gofile.io/?c=8z0vE5

由于以下代码,自定义标记消失了.当我在下面的代码中注释时,标记不会从地图上消失.

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()

请指导我如何更新已添加的Clustermanager项?

Please guide me how to update already added Clustermanager item?

预先感谢

推荐答案

对我有用的一件事是在渲染器级别禁用动画,即GMUClusterRenderer.animatesClusters = false

One thing that worked for me was to disable animations at the renderer level, i.e., GMUClusterRenderer.animatesClusters = false

这篇关于自定义标记在放大地图时消失,并在通过聚类缩小地图时出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:29