Custom Markers Disappear on Zoomin The Map and Appear on Zoomout The Map with Clustering

Moved marker disappears when using Marker Clusterer

This is a bug that has been reported to the google maps api team (issue # 167). See here

It is triggered any time by a setMap(map) followed by any sort of marker movement (including animation) followed by a setMap(null). At that point the marker cannot be shown again.

Google Map with Cluster and Custom view marker lagging too much while zoomIn and zoomOut

Here I fix the map lagging issue and for that I have done the following things.

  • Instead of loading all marker iconView at the starting, I load it inside willRenderMarker so it initially load the 1 or 2 marker iconView and when I zoomin the map clustering expand and marker iconView displays.

Here is the working code.

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

Google maps: Same positioned clustered markers disappear on zoom

Finally found the answer:

Marker Clusterer accepts a parameter maxZoom that controls the maximum zoom to display clusters. If this value is too high (in my case higher than the possible zoom in that area) it won't display the markers properly.

Playing around with this parameter allows for fine-tuned cluster behaviour, as you zoom into your markers.

Ushahidi - How to make the markers stay on the map on zoom change?

Ushahidi actually uses OpenLayers under the hood -- the images may come from Google, but the Javascript library is pure OpenLayers. These markers are actually generated by adding what is known as a context to the style attribute of the OpenLayers.Layer.Vector and an associated Cluster.Strategy. There is a good example of it working properly here: OpenLayers cluster example and if you look at the Javascript source you will see how it is done: view-source:http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/strategy-cluster-threshold.html by defining a radius in the context of the styleMap.

The reason that your example isn't working is that there is actually a script error being triggered, which is stopping the clustering/styling from being performed. The styles are defined in the file ushahidi.js. If you open a Javascript debugger you will see the error for yourself that is firing before the ushahidi.js functions are called to determine the number of features in the cluster and therefore the radius of the markers.

The actual error is http://ti5.net.br/provedorlegal.com.br/index.php/json/cluster?s=1401591600&e=1404183599&z=9, but I have no idea why, as the OpenLayers is the minified version which is very hard to debug. You might find moving your application to OpenLayers directly will help, as it will be easier to debug, and it also works on mobiles, which I know is one of the reasons people like to use Ushahidi.

MarkerClusterer zooming issue

It's more a bug in the Maps-API than a bug in markerClusterer, but you can fix it in markerClusterer.js

I'm not sure where you click on when you(try to) set the zoom to 0(the issue for me doesn't occur when I use the zoom-control), but it happens when I set the zoom using map.setZoom(0)

The issue: the API reports a zoom of 0, but this is incorrect, because the zoom will be set to 1(the minZoom).

Fix:
Replace this part of marcerclusterer.js:

// Add the map event listeners
var that = this;
google.maps.event.addListener(this.map_, 'zoom_changed', function() {
var zoom = that.map_.getZoom();

if (that.prevZoom_ != zoom) {
that.prevZoom_ = zoom;
that.resetViewport();
}
});

...with that:

  // Add the map event listeners
var that = this;
google.maps.event.addListener(this.map_, 'zoom_changed', function() {
var zoom = that.map_.getZoom(),
minZoom=that.map_.minZoom||0,
maxZoom=Math.min(that.map_.maxZoom||100,
that.map_.mapTypes[that.map_.getMapTypeId()].maxZoom);
zoom=Math.min(Math.max(zoom,minZoom),maxZoom);

if (that.prevZoom_ != zoom) {
that.prevZoom_ = zoom;
that.resetViewport();
}
});

Remove a particular marker in google map clustering in iOS

Your code clusterManager.remove(item) does remove the marker from the map, but did you refresh the map? You need to call clusterManager.cluster() again to update the map rendering.

DispatchQueue.main.async {
self.clusterManager.cluster()
}

(Using Swift 3)



Related Topics



Leave a reply



Submit