1

I have got different markers . For some of them, either the longitude or latitude is same .

In case for any of the markers if it has got same longitude or latitude, compared to other markers, I want to increase its latitude size .

But at the end I was ending up changing the coordinates for all the markers

Could you please let me know how to fix this ??

This is my fiddle

This is my code

function checkifgotsameLatitude(response , lator,lotor)
{
    var a = 0;
    for(var i=0;i<response.length;i++)
    {
       var latitusevalue =  response[i].latitude;
           var longitude =  response[i].longititude;
        if(latitusevalue==lator || lotor==longitude)
        {
            a++;
        }
    }
    return a ;
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • in the addMarker function you are taking in lator and lonor which you are not using anywhere. are these two values to be compared with the values in response, or are do you want to compare the values which are within the response object? – Anbarasan Aug 04 '15 at 13:23
  • Thanks , i want to use the first condition only. – Pawan Aug 04 '15 at 13:24

3 Answers3

1

I had a look at your fiddle and I think the solution is to compare item N with other items where the index is greater than N. You can do this by passing the starting index to your checkifgotsameLatitude function.

function checkifgotsameLatitude(response , lator,lotor, startIndex)
{
    for(var i=startIndex;i<response.length;i++)
    {
       var latitusevalue =  response[i].latitude;
           var longitude =  response[i].longititude;
        if(latitusevalue==lator || lotor==longitude)
        {
            return true;
        }
    }
    return false;
}

Updated fiddle

EDIT: the function can now return when the first match is found. this will speed things up. Updated fiddle

Carlos
  • 241
  • 5
  • 11
1

This works if you want to offset one dealer from another if the difference is smaller than 0.0001 or similar

fiddle

function normaliseLat(num) {
    var numDecimals = 4; // precision
    return String(num.toFixed(numDecimals)).replace(".", "_");
}

function diffLng(lng, pos, response) {
    return Math.abs(response[pos].longitude - lng)
}

    var lats = [];
    for (var i = 0; i < response.length; i++) {
       var lat = parseFloat(response[i].latitude);
       var lng = parseFloat(response[i].longitude);
       var norm = normaliseLat(lat);
       var pos = lats.indexOf(norm);
       if (pos!=-1) { // found
         if (diffLng(lng,pos,response) < 0.0002) {
           lat = parseFloat(lat)+0.00001;
         }
       }
       lats[i]=norm;
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Could you please let me know how to implement this You can also subtract them from each other and only offset if the difference is smaller than 0.0001?? – Pawan Aug 05 '15 at 07:27
  • Perhaps this one will help you? http://stackoverflow.com/questions/3548920/google-maps-api-v3-multiple-markers-on-exact-same-spot – mplungjan Aug 05 '15 at 08:24
  • thank you very much , can i just check for the second condition also if the current latitude difference is less than 0.000002 with the values present in the array , that is add this as the second condition to , if (lats.indexOf(parseInt(lat))!=-1) – Pawan Aug 05 '15 at 08:39
  • See update. I did not test the code but you should get the idea – mplungjan Aug 05 '15 at 08:52
  • Uncaught ReferenceError: diff is not defined – Pawan Aug 05 '15 at 08:57
  • I did not test. You need to try to understand and fix any errors in spelling and logic too. The diff is called diffLng. For more support I e-lance in my spare time. – mplungjan Aug 05 '15 at 09:21
  • And function should be `function diffLng(lng,pos, response) {` in that order and you spelled longitude wrong and I had >0.0002 it should be – mplungjan Aug 05 '15 at 09:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85190/discussion-between-preethi-jain-and-mplungjan). – Pawan Aug 05 '15 at 10:01
  • i have just the same fiddle , just altered the response , i am getting Uncaught RangeError: Maximum call stack size exceeded http://jsfiddle.net/mplungjan/x9fupL56/ – Pawan Aug 05 '15 at 10:04
  • http://stackoverflow.com/questions/15671480/uncaught-rangeerror-maximum-call-stack-size-exceeded-google-maps-when-i-try-to - strings and misspelled latitude again. http://jsfiddle.net/mplungjan/x9fupL56/ – mplungjan Aug 05 '15 at 12:08
0

Something that doesn't appear to be accounted for is if after moving the marker, it then matches another marker. Or if you had more than two overlapping markers, all but one would just move to the same new location. I also changed the "or the same" to "and the same" as that part didn't make sense to me, assuming you are trying to adjust them so they don't overlap. But of course that could be changed back easily. I also changed the float equality to use a range.

I believe this corrects these issues:

Fiddle

while(checkifgotsameLatitude(response,lat,lng))
{
    lat = lat+0.222;
    response[i].latitude = lat;
}

function checkifgotsameLatitude(response , lator,lotor)
{
    var a = 0;
    for(var i=0;i<response.length;i++)
    {
       var latitusevalue =  Math.abs(parseFloat(response[i].latitude) - lator) < 0.1;
       var longitude =  Math.abs(parseFloat(response[i].longititude) - lotor) < 0.1;
        if(latitusevalue && longitude)
        {
            a++;
        }
    }
    return a > 1;
}
ryachza
  • 4,460
  • 18
  • 28