如何在不清除GoogleMap的情况下更新多个位置

时间:2019-07-25 12:06:28

标签: swift google-maps google-maps-markers markerclusterer

我在具有集群功能的地图中加载了几个用户,现在我想根据从服务器收到的用户新位置来更新用户位置。

我可以通过清除地图并从服务器加载新用户数据来做到这一点,但看起来标记正在跳跃并且看起来不合适。

有什么方法可以在不清除GoogleMap的情况下更新标记的位置吗?

这是我的问题的链接:Google Map with Cluster and Custom view marker lagging too much while zoomIn and zoomOut

参考屏幕

enter image description here

如果有人从他的位置移开,那么一旦我从服务器接收到更新的数据,如何更新他/她的位置。 (无需清除地图)

我的代码

func setMarkers() {
        for i in 0..<SharedData.sharedInstance.allFriends.count {
            let marker = GMSMarker()
            let friend = SharedData.sharedInstance.allFriends[i]
            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() {
        for i in 0..<arrMarkers.count {
            let marker = arrMarkers[i]
            let friend = SharedData.sharedInstance.allFriends[i]
            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
        }
        self.defaultCamera(latitude: SharedData.sharedInstance.userLocation.coordinate.latitude, longitude: SharedData.sharedInstance.userLocation.coordinate.longitude)
    }

编辑

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


func updateMarkers() {
        for i in 0..<arrMarkers.count {
            let marker = arrMarkers[i]
            let friend = SharedData.sharedInstance.allFriends[i]

            CATransaction.begin()
            CATransaction.setAnimationDuration(1.0)
            marker.position = CLLocationCoordinate2D.init(latitude: friend.user_details.latitude , longitude: friend.user_details.longitude)
            CATransaction.commit()
        }
    }

编辑1

如果我从后端更改了一些用户位置,并且当我获得用户新位置时,我会使用上面的代码更新标记位置,但问题是新位置用户仍然位于旧位置。如果我清除并重绘了用户数据,则可以正常工作。

编辑2

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 !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)
            }

            if markerData.friend.user_details.user_id == 88 {
                print("Will Rendrer Marker: \(markerData.friend.user_details.latitude)")
                print("Will Rendrer Marker: \(markerData.friend.user_details.longitude)")
            }

            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
        }

请指导我这样做。

1 个答案:

答案 0 :(得分:0)

您可以继承GMSMarker的子类,添加对象的ID,然后根据从服务器获取的对象ID获取标记并更新位置。这是一些代码来解释我的意思

p.addMovie(matrix);
personRepository.save(p);

为您的标记添加ID

    class MyMarker: GMSMarker {

         var id: String? = nil

    }

比按ID更新

func setMarkers() {

        let marker = MyMarker()
        let friend = SharedData.sharedInstance.allFriends[I]
        marker.id = friend.id

不要添加标记以再次映射,只需更改其位置