-1

I have two arrays of double values called lat2[] and lon2[]. I have there stored the particular lattitude and longtitude values. And I can get those values as simple double values. My question is how can I find if this two doubles exist in the array (to check which marker has been clicked simply). Here is what I've done so far but it doesnt seem to work:

@Override
public boolean onMarkerClick(Marker marker) {
    markerLatLng = marker.getPosition();
    markerLat = markerLatLng.latitude;
    markerLng = markerLatLng.longitude;
    for (int i = 0; i < lat2.length; i++) {
        if (markerLat == lon2[i]) {
            for (int j = 0; j < lon2.length; j++) {
                if (markerLng == lon2[j]) {
                    String title = marker.getTitle();
                    Log.e("TAG", " " + title);
                }
            }
        }
    }
    return true;
}
Lookie
  • 73
  • 9

2 Answers2

0

Probably if I understood your data structure there is a problem in your for loop.

If you store latitude and longitude in two arrays I imagine that a point in position n is defined by longitude[n] and latitude[n].

If this is how you store your points here is how you need to update your code:

@Override
public boolean onMarkerClick(Marker marker) {
    int markerLat = marker.getPosition().latitude;
    int markerLng = marker.getPosition().longitude;
    for (int i = 0; i < lat2.length; i++) {
        if (markerLat == lat2[i] && markerLng == lon2[j]) {
            String title = marker.getTitle();
            Log.e("TAG", " " + title);               
        }
    }
    return true;
}

Please don't use global variables. I edited your code to define markerLat and markerLng local to the method (I don't know if int is the correct type).

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • You shouldn't compare `double`s via `==`, especially if one of the values is coming from outside your domain of values (i.e. user). You should compare them like this: [`Math.abs(markerLat - lat2[i]) < EPS`](http://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) where EPS is a small number, with lat/lon around `1e-5`. This will give you the answer to "Is marker **close enough** to one of those location stored in `lat2/lon2`?" – TWiStErRob Mar 12 '15 at 17:49
  • But I need to know if they are the exact same value because it is location and even a small change can differ the position – Lookie Mar 13 '15 at 14:18
0

Create a List for example ArrayList from the lat2, log2 array and check if the numbers contains like:

List lat = new ArrayList(lat2);
List lon = new ArrayList(lon2);

if (lat.contains(markerLat) && lon.contains(markerLng)){
            String title = marker.getTitle();
            Log.e("TAG", " " + title);   
}
Laszlo Lugosi
  • 3,669
  • 1
  • 21
  • 17