Google map api - use multiple keywords in place searches
In order to google Place Search and Place Library documentation there no option to use multiple keywords for searching.
Therefore I have done this;
I've created two PlaceService objects because I need to look at 'Pizza Hut' and 'KFC' both in single click.
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['KFC']
}, callback);
var service2 = new google.maps.places.PlacesService(map);
service2.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['Pizzahut']
}, callback);
and It's working well.
Full source code:
<!DOCTYPE html>
<html>
<head>
<title>
Map
</title>
<script async="" defer="" src="https://maps.googleapis.com/maps/api/js?key=[use-your-api-key]&libraries=places">
</script>
<script type="text/javascript">
var map;
var infowindow;
function initMap() {
var pyrmont = 0;
pyrmont = new google.maps.LatLng(-37.8274134, 144.9352466)
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['KFC']
}, callback);
var service2 = new google.maps.places.PlacesService(map);
service2.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['Pizzahut']
}, callback);
var service3 = new google.maps.places.PlacesService(map);
service3.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['Subway']
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
document.onreadystatechange = function() {
var state = document.readyState// you can use jquery document.ready for this
if (state == 'complete') {
initMap();
}
}
</script>
</head>
<body style="margin: 0 !important; padding: 0!important">
<div style="margin: 0 !important">
<div id="map" style="width: 100%;height: 100%;position: absolute;display: block;">
</div>
</div>
</body>
</html>
Therefore I have done this;
I've created two PlaceService objects because I need to look at 'Pizza Hut' and 'KFC' both in single click.
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['KFC']
}, callback);
var service2 = new google.maps.places.PlacesService(map);
service2.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['Pizzahut']
}, callback);
and It's working well.
KFC Layer |
KFC + Pizza Hut Layer |
KFC + Pizza Hut + Subway Layer |
Full source code:
<!DOCTYPE html>
<html>
<head>
<title>
Map
</title>
<script async="" defer="" src="https://maps.googleapis.com/maps/api/js?key=[use-your-api-key]&libraries=places">
</script>
<script type="text/javascript">
var map;
var infowindow;
function initMap() {
var pyrmont = 0;
pyrmont = new google.maps.LatLng(-37.8274134, 144.9352466)
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['KFC']
}, callback);
var service2 = new google.maps.places.PlacesService(map);
service2.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['Pizzahut']
}, callback);
var service3 = new google.maps.places.PlacesService(map);
service3.nearbySearch({
location: pyrmont,
radius: 50000,
keyword: ['Subway']
}, callback);
}
function callback(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
document.onreadystatechange = function() {
var state = document.readyState// you can use jquery document.ready for this
if (state == 'complete') {
initMap();
}
}
</script>
</head>
<body style="margin: 0 !important; padding: 0!important">
<div style="margin: 0 !important">
<div id="map" style="width: 100%;height: 100%;position: absolute;display: block;">
</div>
</div>
</body>
</html>
Comments
Post a Comment