带有集群和自定义视图标记的谷歌地图在放大和缩小时滞后太多
Posted
技术标签:
【中文标题】带有集群和自定义视图标记的谷歌地图在放大和缩小时滞后太多【英文标题】:Google Map with Cluster and Custom view marker lagging too much while zoomIn and zoomOut 【发布时间】:2019-11-24 04:19:12 【问题描述】:我想用自定义视图加载GoogleMap
来代替GMSMarker
并想显示集群,我已经做到了。但是我在放大或缩小地图时面临滞后和内存使用问题。
我已将自定义视图加载到GMSMarker
infoView
这是我的代码。
class InitialMapViewController: UIViewController, GMUClusterManagerDelegate, GMUClusterRendererDelegate
private var clusterManager: GMUClusterManager!
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad()
super.viewDidLoad()
self.initializeClusterItems()
self. setMapView()
//MARK:- Map view actions
func setMapView()
mapView.delegate = self
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
mapView.mapType = .normal
self.mapView.camera = self.defaultCamera(latitude: SharedData.sharedInstance.userLocation.coordinate.latitude, longitude: SharedData.sharedInstance.userLocation.coordinate.longitude)
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated)
self.getAllFriendsList()
//MARK: INITIALIZE CLUSTER ITEMS
func initializeClusterItems()
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUGridBasedClusterAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
renderer.delegate = self
self.clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker)
guard let _ = marker.userData as? POIItem else return
if UIImage(named: "profile_circle_gradiant") != nil
marker.iconView = UIImageView(image: nil, highlightedImage: nil)
private func clusterManager(clusterManager: GMUClusterManager, didTapCluster cluster: GMUCluster)
func defaultCamera(latitude: Double, longitude: Double) -> GMSCameraPosition
let camera = GMSCameraPosition.camera(withLatitude: latitude,
longitude: longitude,
zoom: 16)
return camera
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.icon = imgTypeDoctor
marker.accessibilityHint = String(i)
let infoWindow = Bundle.main.loadNibNamed("HomeMapInfoView", owner: self, options: nil)?.first as! HomeMapInfoView
//infoWindow.imgUser.sd_setImage(with: URL(string: friend.user_details.user_photo), placeholderImage: #imageLiteral(resourceName: "User_profile"), options: .highPriority, completed: nil)
infoWindow.imgCar.image = UIImage.init(named: "small_" + friend.user_details.car_personality_name)
infoWindow.imgCar1.image = UIImage.init(named: "small_" + friend.user_details.car_personality_name)
// infoWindow.lblName.text = friend.user_details.name
// infoWindow.lblUsername.text = "@" + friend.user_details.screen_name
//KULDEEP 01/03/2019
infoWindow.btnImgVW.tag = Int(marker.accessibilityHint!) ?? 0
infoWindow.isUserInteractionEnabled = false
infoWindow.btnImgVW.addTarget(self, action: #selector(btnUserTapped(_:)), for: .touchUpInside)
marker.iconView = infoWindow
marker.tracksViewChanges = true
//marker.userData = friend
marker.map = mapView
//mapView.selectedMarker = marker
//print(i)
self.generatePOIItems(String(format: "%d", i), position: marker.position, icon: nil)
self.clusterManager.cluster()
//self.clusterManager.setDelegate(self, mapDelegate: self)
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition)
UIView.animate(withDuration: 5.0, animations: () -> Void in
//self.londonView?.tintColor = .blue
, completion: (finished) in
// Stop tracking view changes to allow CPU to idle.
//mapView.selectedMarker?.tracksViewChanges = false
)
func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker?
let marker = GMSMarker()
if object is POIItem
// set image view for gmsmarker
return marker
func generatePOIItems(_ accessibilityLabel: String, position: CLLocationCoordinate2D, icon: UIImage?)
let item = POIItem(position: position, name: accessibilityLabel)
self.clusterManager.add(item)
请指导我为什么放大或缩小地图时地图滞后,或者我在此代码中缺少什么?
【问题讨论】:
您需要同时激活它们吗? @HarryJ,是的,这就是我用自定义视图替换 Marker iconView 的原因。 【参考方案1】:在这里我修复了地图滞后问题,为此我做了以下事情。
我没有在开始时加载所有标记 iconView,而是将其加载到willRenderMarker
中,因此它最初加载 1 或 2 个标记 iconView,当我放大地图集群时,会展开并显示标记 iconView。
这是工作代码。
class InitialMapViewController: UIViewController, GMUClusterManagerDelegate, GMUClusterRendererDelegate
private var clusterManager: GMUClusterManager!
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad()
super.viewDidLoad()
self.initializeClusterItems()
self. setMapView()
//MARK:- Map view actions
func setMapView()
mapView.delegate = self
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
mapView.mapType = .normal
self.mapView.camera = self.defaultCamera(latitude: SharedData.sharedInstance.userLocation.coordinate.latitude, longitude: SharedData.sharedInstance.userLocation.coordinate.longitude)
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated)
self.getAllFriendsList()
//MARK: INITIALIZE CLUSTER ITEMS
func initializeClusterItems()
let iconGenerator = GMUDefaultClusterIconGenerator()
let algorithm = GMUGridBasedClusterAlgorithm()
let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
renderer.delegate = self
self.clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker)
marker.groundAnchor = CGPoint(x: 0.5, y: 1)
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)
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 clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool
let newCamera = GMSCameraPosition.camera(withTarget: cluster.position,
zoom: mapView.camera.zoom + 1)
let update = GMSCameraUpdate.setCamera(newCamera)
mapView.moveCamera(update)
return false
func defaultCamera(latitude: Double, longitude: Double) -> GMSCameraPosition
let camera = GMSCameraPosition.camera(withLatitude: latitude,
longitude: longitude,
zoom: 16)
return camera
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 = nil
marker.tracksViewChanges = true
marker.map = mapView
self.generatePOIItems(String(format: "%d", i), position: marker.position, icon: nil, friend: friend, userIndex: i)
clusterManager.cluster()
clusterManager.setDelegate(self, mapDelegate: self)
func generatePOIItems(_ accessibilityLabel: String, position: CLLocationCoordinate2D, icon: UIImage?)
let item = POIItem(position: position, name: accessibilityLabel)
self.clusterManager.add(item)
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
if marker.accessibilityHint != nil
let friend = SharedData.sharedInstance.allFriends[Int(marker.accessibilityHint!) ?? 0]
objSelectedUser = friend
self.setUserData(friend: objSelectedUser)
self.showUserLatestBeeppView()
return true
【讨论】:
以上是关于带有集群和自定义视图标记的谷歌地图在放大和缩小时滞后太多的主要内容,如果未能解决你的问题,请参考以下文章