How to check whether a marker already exists or not in Google maps?

I have latitude and longitude of a place. I want to check whether such a marker is already present or not. How can I do it?

var myLatLng = new google.maps.LatLng(Lat, Long);
//before setting marker i want to check here
marker.setPosition(myLatLng);
marker.setVisible(true);

Is it possible?

Answer

Whenever you add a marker to the map (it’s also best to add the markers to an markers array at this point), add the lat and lng to a separate lookup array.

var lookup = [];
lookup.push([lat, lng]);
marker.setPosition(myLatLng);

Then when you want to check to see if a marker is present at a particular location, loop through the lookup array:

var search = [51.5945434570313, -0.10856299847364426];

function isLocationFree(search) {
  for (var i = 0, l = lookup.length; i < l; i++) {
    if (lookup[i][0] === search[0] && lookup[i][1] === search[1]) {
      return false;
    }
  }
  return true;
}

isLocationFree(search);