Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions samples/place-nearby-search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Google Maps JavaScript Sample

This sample is generated from @googlemaps/js-samples located at
https://github.com/googlemaps-samples/js-api-samples.

## Setup

### Before starting run:

`npm i`

### Run an example on a local web server

First `cd` to the folder for the sample to run, then:

`npm start`

### Build an individual example

From `samples/`:

`npm run build --workspace=sample-name/`

### Build all of the examples.

From `samples/`:

`npm run build-all`

## Feedback

For feedback related to this sample, please open a new issue on
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
23 changes: 23 additions & 0 deletions samples/place-nearby-search/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<!-- [START maps_place_nearby_search] -->
<html>
<head>
<title>Nearby Search</title>

<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="map"></div>

<!-- prettier-ignore -->
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "beta"});</script>
</body>
</html>
<!-- [END maps_place_nearby_search] -->
78 changes: 78 additions & 0 deletions samples/place-nearby-search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license
* Copyright 2024 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// [START maps_place_nearby_search]
let map;

async function initMap() {
const { Map, InfoWindow } = await google.maps.importLibrary('maps') as google.maps.MapsLibrary;

let center = new google.maps.LatLng(52.369358, 4.889258);

map = new Map(document.getElementById('map') as HTMLElement, {
center: center,
zoom: 11,
mapId: 'DEMO_MAP_ID',
mapTypeControl: false,
});
nearbySearch();
}

async function nearbySearch() {
//@ts-ignore
const { Place, SearchNearbyRankPreference } = await google.maps.importLibrary('places') as google.maps.PlacesLibrary;
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary;
// [START maps_place_nearby_search_request]

// Restrict within the map viewport.
let center = new google.maps.LatLng(52.369358, 4.889258);

const request = {
// required parameters
fields: ['displayName', 'location', 'businessStatus'],
locationRestriction: {
center: center,
radius: 500,
},
// optional parameters
includedPrimaryTypes: ['restaurant'],
maxResultCount: 5,
rankPreference: SearchNearbyRankPreference.POPULARITY,
language: 'en-US',
region: 'us',
};

//@ts-ignore
const { places } = await Place.searchNearby(request);
// [END maps_place_nearby_search_request]

if (places.length) {
console.log(places);

const { LatLngBounds } = await google.maps.importLibrary("core") as google.maps.CoreLibrary;
const bounds = new LatLngBounds();

// Loop through and get all the results.
places.forEach((place) => {
const markerView = new AdvancedMarkerElement({
map,
position: place.location,
title: place.displayName,
});

bounds.extend(place.location as google.maps.LatLng);
console.log(place);
});

map.fitBounds(bounds);

} else {
console.log("No results");
}
}

initMap();
// [END maps_place_nearby_search]
14 changes: 14 additions & 0 deletions samples/place-nearby-search/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@js-api-samples/place-nearby-search",
"version": "1.0.0",
"scripts": {
"build": "tsc && bash ../jsfiddle.sh place-nearby-search && bash ../app.sh place-nearby-search && bash ../docs.sh place-nearby-search && npm run build:vite --workspace=. && bash ../dist.sh place-nearby-search",
"test": "tsc && npm run build:vite --workspace=.",
"start": "tsc && vite build --base './' && vite",
"build:vite": "vite build --base './'",
"preview": "vite preview"
},
"dependencies": {

}
}
19 changes: 19 additions & 0 deletions samples/place-nearby-search/place-nearby-search.jshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

{% spaceless %}{% include "maps/documentation/javascript/examples/full/_apikey.html" %}{% endspaceless %}<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Place Nearby Search</title>
<style>
{% includecode content_path="maps/documentation/javascript/examples/samples/place-nearby-search/style.css" region_tag="maps_place_nearby_search" html_escape="False" %}
</style>
</head>
<body>
{% includecode content_path="maps/documentation/javascript/examples/samples/place-nearby-search/index.html" region_tag="maps_place_nearby_search_body" html_escape="False" %}
{{ api_loader_dynamic }}
<script>
{% includecode content_path="maps/documentation/javascript/examples/samples/place-nearby-search/index.js" region_tag="maps_place_nearby_search" html_escape="False" %}
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions samples/place-nearby-search/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* [START maps_place_nearby_search] */
/*
* Always set the map height explicitly to define the size of the div element
* that contains the map.
*/
#map {
height: 100%;
}

/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}

/* [END maps_place_nearby_search] */
17 changes: 17 additions & 0 deletions samples/place-nearby-search/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"strict": true,
"noImplicitAny": false,
"lib": [
"es2015",
"esnext",
"es6",
"dom",
"dom.iterable"
],
"moduleResolution": "Node",
"jsx": "preserve"
}
}