Devin Norgarb Devin Norgarb - 1 year ago 85
Javascript Question

How to customize results returned in nearby place search, Google Maps JS API V3

I am still relatively new to Google Maps API and JS so this might (probably)
have a simple answer.

I am now returning all places within a certain radius from where my location
is set but I want to be more specific such as only plant nursery's, fuel stations and gyms and only display those markers.

Sorry about the long code block, here is a JSBin if you'd prefer,js,output

Thanks in advance.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<script type="text/javascript"

<input id="findMe" type="button" value="find closest place">
<div id="map-canvas" style="height:500px;"></div>


jQuery(function($) {
var $overlay = $('.overlay'),
resize = true,
var service;
var marker = [];
var pos;
var infowindow;
var placeLoc

function initialize() {
var mapOptions = {
zoom: 15
map = new google.maps.Map(document.getElementById('map-canvas'),

// Try HTML5 geolocation
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$('#findMe').data('pos', pos);
var request = {
location: pos,
radius: 1000,
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'You Are Here'
}, function() {
} else {
// Browser doesn't support Geolocation

function callback(results, status) {
var markers = [];
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
$('#findMe').data('markers', markers);

function createMarker(place) {
placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
type: ['store'],
position: place.geometry.location,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 8,
fillColor: '00a14b',
fillOpacity: 0.3,
fillStroke: '00a14b',
strokeWeight: 4,
strokeOpacity: 0.7
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(;, this);
return marker;

function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';

var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
var infowindow = new google.maps.InfoWindow(options);
google.maps.event.addDomListener(window, 'load', initialize);

$('#show').click(function() {
if (resize) {
google.maps.event.trigger(map, 'resize');
resize = false;

$('.overlay-bg').click(function() {

$("#findMe").click(function() {
var pos = $(this).data('pos'),
markers = $(this).data('markers'),
if (!pos || !markers) {
alert('pos or markers not set yet');
$.each(markers, function() {
var distance = google.maps.geometry.spherical.computeDistanceBetween(this.getPosition(), pos);
if (!closest || closest.distance > distance) {
closest = {
marker: this,
distance: distance
if (closest) {
google.maps.event.trigger(closest.marker, 'click')

Answer Source

Its quite simple. You need to pass an array of types that you need to edit your request with a new attribute called types to filter. Ex types: ['bank', 'gym']

Code Block below and I will attached a modified version of your JS Bin

JS Bin

Places types can be find from the links below

Place Types

Google Places API Documentation -

var request = {
            location: pos,
            radius: 1000,
            types: ['bank', 'gym']
          infowindow = new google.maps.InfoWindow();
          var service = new google.maps.places.PlacesService(map);
          service.nearbySearch(request, callback);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download