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
1 change: 1 addition & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ <h1>Maps JSAPI Samples</h1>
<li><a href='/samples/routes-get-alternatives/dist'>routes-get-alternatives</a></li>
<li><a href='/samples/routes-get-directions/dist'>routes-get-directions</a></li>
<li><a href='/samples/routes-get-directions-panel/dist'>routes-get-directions-panel</a></li>
<li><a href='/samples/routes-markers/dist'>routes-markers</a></li>
<li><a href='/samples/routes-route-matrix/dist'>routes-route-matrix</a></li>
<li><a href='/samples/test-example/dist'>test-example</a></li>
<li><a href='/samples/ui-kit-customization/dist'>ui-kit-customization</a></li>
Expand Down
13 changes: 13 additions & 0 deletions dist/samples/routes-markers/app/.eslintsrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": [
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"rules": {
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-this-alias": 1,
"@typescript-eslint/no-empty-function": 1,
"@typescript-eslint/explicit-module-boundary-types": 1,
"@typescript-eslint/no-unused-vars": 1
}
}
33 changes: 33 additions & 0 deletions dist/samples/routes-markers/app/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).

22 changes: 22 additions & 0 deletions dist/samples/routes-markers/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<!--
@license
Copyright 2025 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<!--[START maps_routes_markers]-->
<html>
<head>
<title>Get directions</title>

<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<gmp-map center="37.447646, -122.113878" zoom="12"></gmp-map>
<!-- 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_routes_markers]-->
124 changes: 124 additions & 0 deletions dist/samples/routes-markers/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// [START maps_routes_markers]
let mapPolylines: google.maps.Polyline[] = [];
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement;
let innerMap;

// Initialize and add the map.
async function initMap() {
// Request the needed libraries.
await google.maps.importLibrary('maps');

innerMap = await mapElement.innerMap;
innerMap.setOptions({
mapTypeControl: false,
mapId: 'DEMO_MAP_ID',
});

// Call the function after the map is loaded.
google.maps.event.addListenerOnce(innerMap, 'idle', () => {
getDirections();
});
}

async function getDirections() {
//@ts-ignore
// Request the needed libraries.
const [{ Route }, { PinElement }] = await Promise.all([
google.maps.importLibrary('routes'),
google.maps.importLibrary('marker'),
]);

// [START maps_routes_markers_request_full]
// [START maps_routes_markers_request]
// Define routes request with an intermediate stop.
const request = {
origin: 'Parking lot, Christmas Tree Point Rd, San Francisco, CA 94131',
destination: '100 Spinnaker Dr, Sausalito, CA 94965', // We're having a yummy lunch!
intermediates: [{ location: '300 Finley Rd San Francisco, CA 94129' }], // But first, we golf!
travelMode: 'DRIVING',
fields: ['path', 'legs', 'viewport'],
};
// [END maps_routes_markers_request]

// Call computeRoutes to get the directions.
const result = await Route.computeRoutes(request);
if (!result.routes || result.routes.length === 0) {
console.warn("No routes found");
return;
}
// [END maps_routes_markers_request_full]

// [START maps_routes_markers_style_maker]
// Alter style based on marker index.
function markerOptionsMaker(
defaultOptions: google.maps.marker.AdvancedMarkerElementOptions,
//@ts-ignore
waypointMarkerDetails: google.maps.routes.WaypointMarkerDetails
) {
const { index, totalMarkers, leg } = waypointMarkerDetails;

// Style the origin waypoint.
if (index === 0) {
return {
...defaultOptions,
map: innerMap,
content: new PinElement({
glyph: (index + 1).toString(),
glyphColor: 'white',
background: 'green',
borderColor: 'green',
}).element
}
}

// Style all intermediate waypoints.
if (!(index === 0 || index === totalMarkers - 1)) {
return {
...defaultOptions,
map: innerMap,
content: new PinElement({
glyph: (index + 1).toString(),
glyphColor: 'white',
background: 'blue',
borderColor: 'blue',
}).element
}
}

// Style the destination waypoint.
if (index === totalMarkers - 1) {
return {
...defaultOptions,
map: innerMap,
content: new PinElement({
glyph: (index + 1).toString(),
glyphColor: 'white',
background: 'red',
borderColor: 'red',
}).element
}
}

return { ...defaultOptions, map: innerMap };
}

const markers = await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker);
// [END maps_routes_markers_style_maker]


// Fit the map to the route.
innerMap.fitBounds(result.routes[0].viewport);
innerMap.setHeading(270);

// Create polylines and add them to the map.
mapPolylines = result.routes[0].createPolylines();
mapPolylines.forEach((polyline) => polyline.setMap(innerMap));
}

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

}
}
24 changes: 24 additions & 0 deletions dist/samples/routes-markers/app/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/* [START maps_routes_markers] */
/*
* 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_routes_markers] */
17 changes: 17 additions & 0 deletions dist/samples/routes-markers/app/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"
}
}
5 changes: 5 additions & 0 deletions dist/samples/routes-markers/dist/assets/index-BP2BsdRL.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions dist/samples/routes-markers/dist/assets/index-kz-ac4rW.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* @license
* Copyright 2025 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/#map{height:100%}html,body{height:100%;margin:0;padding:0}
22 changes: 22 additions & 0 deletions dist/samples/routes-markers/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<!--
@license
Copyright 2025 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<!--[START maps_routes_markers]-->
<html>
<head>
<title>Get directions</title>

<script type="module" crossorigin src="./assets/index-BP2BsdRL.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-kz-ac4rW.css">
</head>
<body>
<gmp-map center="37.447646, -122.113878" zoom="12"></gmp-map>
<!-- 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_routes_markers]-->
22 changes: 22 additions & 0 deletions dist/samples/routes-markers/docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<!--
@license
Copyright 2025 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<!--[START maps_routes_markers]-->
<html>
<head>
<title>Get directions</title>

<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<gmp-map center="37.447646, -122.113878" zoom="12"></gmp-map>
<!-- 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_routes_markers]-->
Loading