jamesteague
jamesteague

Reputation: 47

Trying to use the place attribute for showing/hiding markers in Google Maps API

Ok, almost solved. I am trying to use the place attribute from a nearbySearch (shown below)

var request = {
  location: pyrmont,
  radius: '500',
  types: ['atm','bus_station','parking']
};
infowindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);

The markers are stored in an array ( below)

    function createMarker(place) {
      var iconType = {};
      iconType['atm'] = "http://maps.google.com/mapfiles/ms/micons/dollar.png";
      iconType['bus_station'] = "http://maps.google.com/mapfiles/ms/micons/bus.png";
      //iconType['restaurant'] = "http://maps.google.com/mapfiles/ms/micons/restaurant.png";
      iconType['parking'] = "http://maps.google.com/mapfiles/ms/micons/parkinglot.png";
      var placeLoc = place.geometry.location;
      var marker = new google.maps.Marker({
      map : map,
      icon : iconType[place.types[0]],
      position : place.geometry.location,
      types : place.types
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.setContent(place.name);
      infowindow.open(map, this);
    });

    return marker;
  }

Now the problem is that the 'if' statement below is not matching the place in the array, with the string I enter, Im not sure if the statement itself is wrong, or if I am not retrieving the place.types attribute correctly from the Google request.

      function onRemoveBtn_Clicked() {
    var i;
    for (i = 0; i < markers.length; i++) {
      if (markers[i].get("types") != ATM) {
      markers[i].setMap(null);
    }
  }

I have had great help with this problem up until now and would like to thank all those who have taken the time to help me, this is the last that I will bother you :)

Upvotes: 0

Views: 192

Answers (1)

wf9a5m75
wf9a5m75

Reputation: 6158

If you set an array of types to a marker, marker.get("types") return the array. So the code should be like this:

function onRemoveBtn_Clicked() {
  var i, j, isHit, types;
  for (i = 0; i < markers.length; i++) {
    types = markers[i].get("types");
    if (types) {
      isHit = false;
      for (j = 0; j < types.length; j++) {
        if (types[j] == "atm") {
          isHit = true;
          break;
        }
      }
      if (isHit) {
        markers[i].setMap(null);
      }
    }
  }
}

Upvotes: 1

Related Questions