Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some good examples of this already. <a href="http://googlegeodevelopers.blogspot.co.uk/2012/05/google-places-api-search-refinements-as.html" rel="noreferrer">http://googlegeodevelopers.blogspot.co.uk/2012/05/google-places-api-search-refinements-as.html</a></p> <p>Just start with the basics and work your way up <a href="https://developers.google.com/places/documentation/search" rel="noreferrer">https://developers.google.com/places/documentation/search</a></p> <p>Your best bet is to break up the problem into a factory model. Work on developing functions that get results from the location. Then push the results through to a function that maps this on the page.</p> <p><code></p> <pre><code>&lt;script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&amp;libraries=places"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var map, places, iw; var markers = []; var searchTimeout; var centerMarker; var autocomplete; var hostnameRegexp = new RegExp('^https?://.+?/'); function initialize() { var myLatlng = new google.maps.LatLng(37.786906,-122.410156); var myOptions = { zoom: 15, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); places = new google.maps.places.PlacesService(map); google.maps.event.addListener(map, 'tilesloaded', tilesLoaded); document.getElementById('keyword').onkeyup = function(e) { if (!e) var e = window.event; if (e.keyCode != 13) return; document.getElementById('keyword').blur(); search(document.getElementById('keyword').value); } var typeSelect = document.getElementById('type'); typeSelect.onchange = function() { search(); }; var rankBySelect = document.getElementById('rankBy'); rankBySelect.onchange = function() { search(); }; } function tilesLoaded() { search(); google.maps.event.clearListeners(map, 'tilesloaded'); google.maps.event.addListener(map, 'zoom_changed', searchIfRankByProminence); google.maps.event.addListener(map, 'dragend', search); } function searchIfRankByProminence() { if (document.getElementById('rankBy').value == 'prominence') { search(); } } function search() { clearResults(); clearMarkers(); if (searchTimeout) { window.clearTimeout(searchTimeout); } searchTimeout = window.setTimeout(reallyDoSearch, 500); } function reallyDoSearch() { var type = document.getElementById('type').value; var keyword = document.getElementById('keyword').value; var rankBy = document.getElementById('rankBy').value; var search = {}; if (keyword) { search.keyword = keyword; } if (type != 'establishment') { search.types = [type]; } if (rankBy == 'distance' &amp;&amp; (search.types || search.keyword)) { search.rankBy = google.maps.places.RankBy.DISTANCE; search.location = map.getCenter(); centerMarker = new google.maps.Marker({ position: search.location, animation: google.maps.Animation.DROP, map: map }); } else { search.bounds = map.getBounds(); } places.search(search, function(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i &lt; results.length; i++) { var icon = 'icons/number_' + (i+1) + '.png'; markers.push(new google.maps.Marker({ position: results[i].geometry.location, animation: google.maps.Animation.DROP, icon: icon })); google.maps.event.addListener(markers[i], 'click', getDetails(results[i], i)); window.setTimeout(dropMarker(i), i * 100); addResult(results[i], i); } } }); } function clearMarkers() { for (var i = 0; i &lt; markers.length; i++) { markers[i].setMap(null); } markers = []; if (centerMarker) { centerMarker.setMap(null); } } function dropMarker(i) { return function() { if (markers[i]) { markers[i].setMap(map); } } } function addResult(result, i) { var results = document.getElementById('results'); var tr = document.createElement('tr'); tr.style.backgroundColor = (i% 2 == 0 ? '#F0F0F0' : '#FFFFFF'); tr.onclick = function() { google.maps.event.trigger(markers[i], 'click'); }; var iconTd = document.createElement('td'); var nameTd = document.createElement('td'); var icon = document.createElement('img'); icon.src = 'icons/number_' + (i+1) + '.png'; icon.setAttribute('class', 'placeIcon'); icon.setAttribute('className', 'placeIcon'); var name = document.createTextNode(result.name); iconTd.appendChild(icon); nameTd.appendChild(name); tr.appendChild(iconTd); tr.appendChild(nameTd); results.appendChild(tr); } function clearResults() { var results = document.getElementById('results'); while (results.childNodes[0]) { results.removeChild(results.childNodes[0]); } } function getDetails(result, i) { return function() { places.getDetails({ reference: result.reference }, showInfoWindow(i)); } } function showInfoWindow(i) { return function(place, status) { if (iw) { iw.close(); iw = null; } if (status == google.maps.places.PlacesServiceStatus.OK) { iw = new google.maps.InfoWindow({ content: getIWContent(place) }); iw.open(map, markers[i]); } } } function getIWContent(place) { var content = ''; content += '&lt;table&gt;'; content += '&lt;tr class="iw_table_row"&gt;'; content += '&lt;td style="text-align: right"&gt;&lt;img class="hotelIcon" src="' + place.icon + '"/&gt;&lt;/td&gt;'; content += '&lt;td&gt;&lt;b&gt;&lt;a href="' + place.url + '"&gt;' + place.name + '&lt;/a&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;'; content += '&lt;tr class="iw_table_row"&gt;&lt;td class="iw_attribute_name"&gt;Address:&lt;/td&gt;&lt;td&gt;' + place.vicinity + '&lt;/td&gt;&lt;/tr&gt;'; if (place.formatted_phone_number) { content += '&lt;tr class="iw_table_row"&gt;&lt;td class="iw_attribute_name"&gt;Telephone:&lt;/td&gt;&lt;td&gt;' + place.formatted_phone_number + '&lt;/td&gt;&lt;/tr&gt;'; } if (place.rating) { var ratingHtml = ''; for (var i = 0; i &lt; 5; i++) { if (place.rating &lt; (i + 0.5)) { ratingHtml += '&amp;#10025;'; } else { ratingHtml += '&amp;#10029;'; } } content += '&lt;tr class="iw_table_row"&gt;&lt;td class="iw_attribute_name"&gt;Rating:&lt;/td&gt;&lt;td&gt;&lt;span id="rating"&gt;' + ratingHtml + '&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;'; } if (place.website) { var fullUrl = place.website; var website = hostnameRegexp.exec(place.website); if (website == null) { website = 'http://' + place.website + '/'; fullUrl = website; } content += '&lt;tr class="iw_table_row"&gt;&lt;td class="iw_attribute_name"&gt;Website:&lt;/td&gt;&lt;td&gt;&lt;a href="' + fullUrl + '"&gt;' + website + '&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;'; } content += '&lt;/table&gt;'; return content; } google.maps.event.addDomListener(window, 'load', initialize); </code></pre> <p></code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload