1

I have got a array of Markers

var markers = [{
    "longititude": "78.377670",
    "latitude": "17.439669",
    "name": "one"
}, {
    "longititude": "78.377617",
    "latitude": "17.439692",
    "name": "two"
}, {
    "longititude": "78.377644",
    "latitude": "17.439674",
    "name": "three"
}, {
    "longititude": "78.377665",
    "latitude": "17.439667",
    "name": "four"
}]

I am sending latitude and longitude and checking if it falls under a range of certain distance .

In response from the function i am expecting the total count and the latitude and longitude values under which it falls .

But i am always getting the last element of the array

http://jsfiddle.net/1nm9adn3/1/

Could you please let me know how to resolve this issue .

3
  • It's better to post your Javascript function directly in your question. Commented Aug 6, 2015 at 14:19
  • In simple i need the first value , but where as now i am getting the last value Commented Aug 6, 2015 at 14:22
  • Look this version of your fiddle jsfiddle.net/1nm9adn3/2 Commented Aug 6, 2015 at 14:23

4 Answers 4

1

You replace the value of latitude and longitude in your function every time. So by the time the loop ends, var latitude and var longitude will always have the values of the last element in your array. You should save the value of i when your if clause detects the correct marker and then return markers[i].latitude.

Sign up to request clarification or add additional context in comments.

1 Comment

i am unable to achieve that , could you please let me know how to do this
1

did you mean something like this : http://jsfiddle.net/1nm9adn3/4/

function fetchValue(markers, lat, lng) {
    var result = {    
        count: 0,
        values: []
    };

    for (var i = 0; i < markers.length; i++) {
        var latitude = markers[i].latitude;
        var longitude = markers[i].longititude;
        if (lat == latitude && lng == longitude) {
            result.count++;
            result.values.push({latitude: latitude, longitude: longitude});
        }
        else if (Math.sqrt(Math.pow((lat - latitude), 2) + Math.pow((lng - longitude), 2)) < 0.000005) {
            result.count++;
            result.values.push({latitude: latitude, longitude: longitude});
        }
    }

    return result;
}

Comments

1

I am not sure about why you want to respond with the first matching element, instead of the nearest element, but this would do the trick anyway:

function fetchValue(markers, lat, lng) {
var count = 0;
var firstMatchingLatitude = null;
var firstMatchingLongititude = null;
for (var i = 0; i < markers.length; i++) {
    var latitude = markers[i].latitude;
    var longitude = markers[i].longititude;
    if (lat == latitude && lng == longitude) {
        if(firstMatchingLatitude == null){
            firstMatchingLatitude = latitude;
            firstMatchingLongititude = longitude;
        }
        count++;
    }
    else if (Math.sqrt(Math.pow((lat - latitude), 2) + Math.pow((lng - longitude), 2)) < 0.000005) {
        if(firstMatchingLatitude == null){
            firstMatchingLatitude = latitude;
            firstMatchingLongititude = longitude;
        }
        count++;
    }
}
var result = {    
    count: count,
    latitude: firstMatchingLatitude,
    longitude: firstMatchingLongititude
};
return result;
}

The trick is that you were setting latitude and longititude in every repetition of the for loop. Oh, and it's "latitude" instead of "latititude" ;)

Good luck with that!!

Comments

0
var markers = [
{
    "longititude": "78.377665",
    "latitude": "17.439669",
    "name": "one"
},
{
    "longititude": "78.377617",
    "latitude": "17.439692",
    "name": "two"
},
{
    "longititude": "78.377644",
    "latitude": "17.439674",
    "name": "three"
},
{
    "longititude": "78.377665",
    "latitude": "17.439667",
   "name": "four"
 }
]

 function fetchValue(markers, lat, lng) {
 var count = 0;
 var latitude = [];
 var longitude =[];
 for (var i = 0; i < markers.length; i++) {
     var latit = markers[i].latitude;
   var longit = markers[i].longititude;
    if (lat == latit && lng == longit) {
        count++;
        latitude.push(latit);
        longitude.push(longit);            
    }
    else if (Math.sqrt(Math.pow((lat - latitude), 2) + Math.pow((lng - longitude), 2)) < 0.000005) {
        count++;
    }
}
var result = {    
    count: count,
    latitude: latitude,
    longitude: longitude
};
return result;
 }
 var resltval = fetchValue(markers,17.439669,78.377665);
 alert(JSON.stringify(resltval));

Comments