Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 655
just_a_dev Avatar asked Jul 20 '17 12:07

just_a_dev


3 Answers

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!)
like image 76
Arpit Jain Avatar answered Oct 27 '22 20:10

Arpit Jain


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.

like image 37
HairballsCleaner Avatar answered Oct 27 '22 21:10

HairballsCleaner


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

like image 39
Rajan Maheshwari Avatar answered Oct 27 '22 19:10

Rajan Maheshwari