1

I'm building a basic car hire search application that returns results from a number of providers, and shows the locations of these providers via a Leaflet map. At the moment, I'm looping through the results and adding a location marker to a Layer Group for each result.

However each provider returns a number of results (different car types that they offer and so on), and I'm trying to work out how I can add just one marker for each provider. I'm guessing that maybe I need to use 'hasLayer' somehow, I'm just not sure how....

ParkerDigital
  • 1,015
  • 3
  • 12
  • 16
  • 1
    `HasLayer` only works if you are comparing the same object. It will not work when you have two separate objects that contain the same data. You would need to write some custom comparison code before adding a new layer - one that checks for these 'duplicate' cars - before adding the new layer to the group. – Patrick D Jun 26 '13 at 18:12

1 Answers1

4

Leaflet only checks if you have already the same marker to the map.

You need to remember everything that you have added and check before you add it.

var added = [];

function addShop(shop){

    if(!added.contains(shop.id)) 
        var marker = magicMarkerFactory(shop);
        map.addTo(marker);
        added.push(shop.id);
    }

}
L. Sanna
  • 6,482
  • 7
  • 33
  • 47
  • 1
    Perfect. Only thing is that there is no array.contains natively. So as a workaround you can use jQuery.inArray() (http://stackoverflow.com/a/1473742/3041394 or) or code it by oneself – schlenger Nov 10 '15 at 08:08