7

I have an arraylist which consists of latLong object as shown below:

ArrayList < LatLng > latLngList = new ArrayList< LatLng >();

Note: LatLng is an object which has latitude and longitude values in double format i.e., Latitude is in double format and Longitude is in double format

The below are the values which are stored in the above latLngList object:

Lat Long List:[lat/lng: (34.072516,-118.403609), lat/lng: (34.074227,-118.399248), lat/lng: (34.07304,-118.3995), lat/lng: (34.07304,-118.3995), lat/lng: (34.07304,-118.3995), lat/lng: (34.067856,-118.401485)]

Now, since i am dealing with google map v2 in android, whenever i click on a particular marker i get the latitude and longitude position and i try to compare it with the latLngList object to find whether the object is present in the latLngList.

I am doing it in the following way:

 for(LatLng latLng : latLngList) {
       marker.getPosition().equals(latLng); **Always returns false**
 }

The marker position is returning me with the following latLng object:

lat/lng: (34.074226888265116,-118.39924816042185)

It is clear that the latitude and longitude object returned by marker is a bit lengthy and hence the comparison fails.

Is there a better way of comparing the latLng object?

krisDrOid
  • 3,252
  • 3
  • 25
  • 36

5 Answers5

6

A better way is not to compare markers based on LatLng, but on the identity of Marker. For some weird reason Marker.getPosition() doesn't return exactly the same value.

I'd suggest storing all markers in a List or Map if you want to attach some additional info to every Marker.

Alternatively you may use Marker.getId() after you create Marker, store it and later compare that in onMarkerClick.

Edit:

I have also reported this on gmaps-api-issues.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
4

Implement latlngs like this. this worked for me.

final LatLng[] latLngs = new LatLng[2];
latLngs[0] = new LatLng(lat, lng);
latLngs[1] = new LatLng(lat, lng);

if(latLngs1[0].equals(latLngs1[1])) {
//to do 
}                                                    
1

I think that the best way is to calculate the distance between your latlng point and markers points, using the static Location method. You can deal with returned distance in meters, and decide if is the same point aproximately.

jgonza73
  • 344
  • 1
  • 10
  • You can order your ArrayList by distance from Marker position. Just implements Comparable interface, so first item of ArrayList is the nearest one. – jgonza73 Mar 25 '14 at 12:14
  • what? you will need to reorder them every time when clicking different markers, so performance will get only worse — from O(n) to O(n*log(n)) – Display Name Mar 25 '14 at 12:24
  • You are right for sure, performance is really bad. I use it just for getting the nearest point from an ArrayList to a LatLng. – jgonza73 Mar 25 '14 at 12:37
0

I want to clarify Maciej's answer a bit, because the question title tells about comparing LatLng objects. LatLng objects are, by themselves, perfectly equatable — their equals and hashCode semantics are correct. (and the docs say this, too) It's only the Marker bug that the Marker's store their positions not accurately.
So if you indeed want to keep track of precise Marker positions, you can simply store all added markers in a HashMap<Marker, LatLng>. But if you want just to compare two markers, don't use their positions at all, a simple Marker.getId() will do the job, as MaciejGórski said.

Display Name
  • 8,022
  • 3
  • 31
  • 66
0

You have 2 options i can think of, one is using the quadtree library: http://koti.mbnet.fi/ojalesa/quadtree/quadtree_intro.htm which is easy to use and ver helpful, the other is a very good answer by @geocodezip (in javascript)

var SameThreshold = 0.1;
if (google.maps.geometry.spherical.computeDistanceBetween(latlng1,latlng2) < SameThreshold)
marker1.setAnimation(google.maps.Animation.BOUNCE); 
Gerardo Rosciano
  • 901
  • 5
  • 11