Skip to content

Instantly share code, notes, and snippets.

@pkrakowiak
Created February 14, 2013 16:10
Show Gist options
  • Save pkrakowiak/4953833 to your computer and use it in GitHub Desktop.
Save pkrakowiak/4953833 to your computer and use it in GitHub Desktop.
Trying to get the Google Places API to work correctly (again). It used to return better results, but now if you type in two or more words in the search box you'll get no results. Works good if you just provide one word, though.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&amp;sensor=false"></script>
</head>
<body>
<input type="text" id="search" style="width: 300px;" /><input type="button" id="searchBtn" value="Search"/><br/>
<div id="map" style="width: 300px; height: 300px;"></div>
<div id="results" style="width: 600px;height: 600px;background-color:gray;color:white;"></div>
<script type="text/javascript">
$(function(){
var indy = new google.maps.LatLng(39.7685825,-86.1579557);
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: indy,
zoom: 10
});
var service = new google.maps.places.PlacesService(map);
$('#searchBtn').click(function() {
$('#results').empty();
var request = {
location: indy,
radius: 5000,
/*types: ['geocode'],*/
name: $('#search').val()
};
service.nearbySearch(request, function(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
printLocationName($.map(results, function (item) {
return {
reference: item.reference,
label: item.formatted_address,
value: item.name,
latitude: item.geometry.location.lat(),
longitude: item.geometry.location.lng()
};
}));
}
if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
$('#results').append('<span>No results.</span><img src="http://i474.photobucket.com/albums/rr104/gio_dim/AVerySadPanda.png"/>')
}
});
});
function printLocationName(places) {
for (var i = 0; i < places.length; i++) {
$('#results').append('<span>' + places[i].value + '</span><br/>');
}
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment