How to handle more than one location on the same address and cluster them?

Viewed 3246

I'm using google maps with clusters in iOS application developed in Swift. On google map there are some markers with the same latitude and longitude. When user zooms in to the point where are only two places in cluster and zooms a little bit more when markers are shown, these markers are glitched (flickering) and they overlay rapidly each other (one over another).

How can I accomplish when user zooms in, and there are only two markers in cluster with same address and location, to not split them out of cluster into markers, so that they stay clustered? Is there a way to get markers from (that are in) the cluster Thanks for your answers.

5 Answers

I am sorry to be late for the party but: as of version 1.4.0(July 2013) of the SDK there's a property on GMSOverlay(Which GMSMarker subclasses) named 'zIndex'.

As stated in the docs:

Higher zIndex value overlays will be drawn on top of lower zIndex value tile layers and overlays.

Equal values result in undefined draw ordering.

The flickering might be because both markers have zIndex == 0

Anyways, that's what worked for me.

Here's a Swift 4 solution extending the CLLocationCoordinate2D based on Arpit Jain answer:

extension CLLocationCoordinate2D {
    mutating func variateForEqual(coordinates: [CLLocationCoordinate2D]) {
        var variatedLatitude = latitude
        var variatedLongitude = longitude

        let filteredPositions = coordinates.filter {
            $0 == self
        }

        if filteredPositions.isNotEmpty {
            let variation = Double.random(in: -0.01...0.01) / 1500
            variatedLatitude = latitude + variation
            variatedLongitude = longitude + variation
        }

        self = CLLocationCoordinate2D(latitude: variatedLatitude, longitude: variatedLongitude)
    }
}
`
Related