Skip to content
Open
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
41 changes: 41 additions & 0 deletions samples/control-bounds-restriction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Google Maps JavaScript Sample

## control-bounds-restriction

This sample demonstrates restricting the map to a specific area.

## Setup

### Before starting run:

`npm i`

### Run an example on a local web server

`cd samples/control-bounds-restriction`
`npm start`

### Build an individual example

`cd samples/control-bounds-restriction`
`npm run build`

From 'samples':

`npm run build --workspace=control-bounds-restriction/`

### Build all of the examples.

From 'samples':

`npm run build-all`

### Run lint to check for problems

`cd samples/control-bounds-restriction`
`npx eslint index.ts`

## Feedback

For feedback related to this sample, please open a new issue on
[GitHub](https://github.com/googlemaps-samples/js-api-samples/issues).
22 changes: 22 additions & 0 deletions samples/control-bounds-restriction/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<!--
@license
Copyright 2026 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<!-- [START maps_control_bounds_restriction] -->
<html>
<head>
<title>Map Bounds Restriction</title>

<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<!-- 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: "weekly"});</script>
</head>
<body>
<gmp-map center="-37.06, 174.58" zoom="7"></gmp-map>
</body>
</html>
<!-- [END maps_control_bounds_restriction] -->
37 changes: 37 additions & 0 deletions samples/control-bounds-restriction/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @license
* Copyright 2026 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// [START maps_control_bounds_restriction]
let innerMap;
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;

// [START maps_control_bounds_restriction_region]
const NEW_ZEALAND_BOUNDS = {
north: -34.36,
south: -47.35,
west: 166.28,
east: -175.81,
};
// [END maps_control_bounds_restriction_region]

async function initMap() {
// Import the needed libraries.
(await google.maps.importLibrary('maps')) as google.maps.MapsLibrary;

innerMap = mapElement.innerMap;
// [START maps_control_bounds_restriction_options]
// Restrict the map to the provided bounds.
innerMap.setOptions({
restriction: {
latLngBounds: NEW_ZEALAND_BOUNDS,
strictBounds: false,
}
});
// [END maps_control_bounds_restriction_options]
}

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

}
}
17 changes: 17 additions & 0 deletions samples/control-bounds-restriction/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright 2026 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* [START maps_control_bounds_restriction] */
/*
* Optional: Makes the sample page fill the window.
*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}

/* [END maps_control_bounds_restriction] */
17 changes: 17 additions & 0 deletions samples/control-bounds-restriction/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"
}
}
Loading