just_a_dev
just_a_dev

Reputation: 183

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

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.

Upvotes: 7

Views: 4448

Answers (5)

Marcel Starczyk
Marcel Starczyk

Reputation: 21

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

Upvotes: 1

RawKnee
RawKnee

Reputation: 323

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.

Upvotes: 5

Rajan Maheshwari
Rajan Maheshwari

Reputation: 14571

I recently faced this issue of multiple happenings on same latitude and longitude. I believe changing minor in latitude and longitude is an alternative but this may violate your business model if you are making a very specific location based application.

One solution is having a callout like view where you can list your multiple happenings when you tap on marker. Keep a custom image of marker with some badge number on top right(preferably) and on tapping the marker, show a callout.

You can refer the screenshot.

enter image description here

Something like this if this place has multiple happenings. You can just show any one happening and then on tap of marker you can show a callout of other happenings. You can make a callout by using a tableView or something.

Hope you get some idea.

enter image description here

Upvotes: 2

Arpit Jain
Arpit Jain

Reputation: 1690

I had the same issue , to overcome flashing effect it had given very minor variation to similar values of latitude and longitude.

func checkIfMutlipleCoordinates(latitude : Float , longitude : Float) -> CLLocationCoordinate2D{

        var lat = latitude
        var lng = longitude

        // arrFilterData is array of model which is giving lat long

        let arrTemp = self.arrFilteredData.filter {

            return (((latitude == $0.latitute) && (longitude == $0.longitute)))
        }

        // arrTemp giving array of objects with similar lat long 

        if arrTemp.count > 1{
            // Core Logic giving minor variation to similar lat long

            let variation = (randomFloat(min: 0.0, max: 2.0) - 0.5) / 1500
            lat = lat + variation
            lng = lng + variation
        }
        let finalPos = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat), longitude: CLLocationDegrees(lng))
        return  finalPos
    }

 func randomFloat(min: Float, max:Float) -> Float {
        return (Float(arc4random()) / 0xFFFFFFFF) * (max - min) + min
    }

Simply call checkIfMutlipleCoordinates whenever you set marker position

 let position = checkIfMutlipleCoordinates(latitude: yourlatitute!, longitude: yourlongitute!)

Upvotes: 14

Manish Pathak
Manish Pathak

Reputation: 3304

Known behavior of Google Maps iOS SDK

If you are creating a custom marker and using the iconView property to change an image of the icon it will give a flashing effect if the coordinates are completely same.It is a known issue of google maps iOS SDK.Try using default marker's instead and see if an issue is persisting.

Upvotes: 2

Related Questions