Please see my quick and dirty Demo . The idea is to use the place.types array to determine which place you want to add to the map. I simplistically assigned a marker to the first element of this array (usually 2 or 3 elements), which might look something like this:
["school", "establishment"]
In some cases, a "university" comes in front of a "school", so the university icon is used. You need to clarify how you map types to icons, that is, to give priority to a school or university? Perhaps iterating through the array is looking for suitable types. One place (general_contractor) is not on my list of icons, so the default output marker is placed by default. You can use the default icon if you checked whether iconType has the type you iconType or not. I will leave this data to you =)
Here is the source I used for the icons: https://sites.google.com/site/gmapsdevelopment/
function createMarker(place) { var placeLoc = place.geometry.location; var iconType = {}; iconType['school'] = "http://maps.google.com/mapfiles/kml/pal2/icon2.png"; iconType['church'] = "http://maps.google.com/mapfiles/kml/pal2/icon11.png"; iconType['park'] = "http://maps.google.com/mapfiles/kml/pal2/icon12.png"; iconType['university'] = "http://maps.google.com/mapfiles/kml/pal2/icon14.png"; var marker = new google.maps.Marker({ map: map, position: place.geometry.location, icon: iconType[place.types[0]] }); google.maps.event.addListener(marker, 'mouseover', function() { infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">'); infowindow.open(map, this); }); }
Alternatively, use the switch statement:
function createMarker(place) { var placeLoc = place.geometry.location; var iconUrl; switch (place.types[0]) { case 'school': iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon2.png"; break; case 'church': iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon11.png"; break; case 'park': iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon12.png"; break; case 'university': iconUrl = "http://maps.google.com/mapfiles/kml/pal2/icon14.png"; break; default: iconUrl = "http://maps.google.com/mapfiles/kml/pal4/icon39.png"; } var marker = new google.maps.Marker({ map: map, position: place.geometry.location, icon: iconUrl }); google.maps.event.addListener(marker, 'mouseover', function() { infowindow.setContent(place.name + '<br/>' + place.vicinity + '<br/><img src="' + place.icon + '">'); infowindow.open(map, this); }); }
Tina CG Hoehr
source share