TypeScript
let map: google.maps.Map;
let localContextMapView;
const styles: google.maps.MapTypeStyle[] = [
{
elementType: "geometry",
stylers: [
{
color: "#f5f5f5",
},
],
},
{
elementType: "labels",
stylers: [
{
visibility: "off",
},
],
},
{
elementType: "labels.icon",
stylers: [
{
visibility: "off",
},
],
},
{
elementType: "labels.text.fill",
stylers: [
{
color: "#616161",
},
],
},
{
elementType: "labels.text.stroke",
stylers: [
{
color: "#f5f5f5",
},
],
},
{
featureType: "administrative.land_parcel",
stylers: [
{
visibility: "off",
},
],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [
{
color: "#bdbdbd",
},
],
},
{
featureType: "administrative.neighborhood",
stylers: [
{
visibility: "off",
},
],
},
{
featureType: "poi",
elementType: "geometry",
stylers: [
{
color: "#eeeeee",
},
],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [
{
color: "#757575",
},
],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [
{
color: "#e5e5e5",
},
],
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [
{
color: "#9e9e9e",
},
],
},
{
featureType: "road",
elementType: "geometry",
stylers: [
{
color: "#ffffff",
},
],
},
{
featureType: "road.arterial",
elementType: "labels.text.fill",
stylers: [
{
color: "#757575",
},
],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [
{
color: "#dadada",
},
],
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [
{
color: "#616161",
},
],
},
{
featureType: "road.local",
elementType: "labels.text.fill",
stylers: [
{
color: "#9e9e9e",
},
],
},
{
featureType: "transit.line",
elementType: "geometry",
stylers: [
{
color: "#e5e5e5",
},
],
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [
{
color: "#eeeeee",
},
],
},
{
featureType: "water",
elementType: "geometry",
stylers: [
{
color: "#c9c9c9",
},
],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [
{
color: "#9e9e9e",
},
],
},
];
function initMap() {
localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [
{ type: "bakery", weight: 1 },
{ type: "bank", weight: 1 },
{ type: "cafe", weight: 2 },
{ type: "department_store", weight: 1 },
{ type: "drugstore", weight: 1 },
{ type: "park", weight: 3 },
{ type: "restaurant", weight: 2 },
{ type: "primary_school", weight: 3 },
{ type: "secondary_school", weight: 3 },
{ type: "supermarket", weight: 2 },
],
maxPlaceCount: 24,
});
map = localContextMapView.map!;
map.setOptions({
center: { lat: 51.507307, lng: -0.08114 },
zoom: 14,
styles,
});
// Build and add the Autocomplete search bar
const input = document.getElementById("input")! as HTMLInputElement;
const options = {
types: ["address"],
componentRestrictions: {
country: "us",
},
fields: ["address_components", "geometry", "name"],
};
const autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.addListener("place_changed", () => {
const place = autocomplete.getPlace();
if (!place || !place.geometry) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert("No address available for that input.");
return;
}
// Recenter the map to the selected address
map.setOptions({
center: place.geometry!.location,
zoom: 14,
});
// Update the localContext directionsOptions origin
localContextMapView.directionsOptions = {
origin: place.geometry!.location,
};
new google.maps.Marker({
position: place.geometry!.location,
map: map,
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAQAAABKfvVzAAAAbUlEQVR4Ae3LoQ2AMAAF0TMYPJoV2IApGIJtmIMtmIAVqutraj6IiqZpmyYoCO/08R7bXbOOHSF2Ohr0HCh00EPdwImiTgYqRgxKMowUTFiUyTKRMeNQIcdMYsGjSp6FyIoaWkmoUuLxEPzDh1xIaLFFuTyHMgAAAABJRU5ErkJggg==",
zIndex: 30,
});
// update the results with new places
localContextMapView.search();
});
}
declare global {
interface Window {
initMap: () => void
}
}
window.initMap = initMap;
참고: TypeScript 및 Google 지도 사용에 관한 가이드 를 읽어 보세요.
자바스크립트
let map;
let localContextMapView;
const styles = [
{
elementType: "geometry",
stylers: [
{
color: "#f5f5f5",
},
],
},
{
elementType: "labels",
stylers: [
{
visibility: "off",
},
],
},
{
elementType: "labels.icon",
stylers: [
{
visibility: "off",
},
],
},
{
elementType: "labels.text.fill",
stylers: [
{
color: "#616161",
},
],
},
{
elementType: "labels.text.stroke",
stylers: [
{
color: "#f5f5f5",
},
],
},
{
featureType: "administrative.land_parcel",
stylers: [
{
visibility: "off",
},
],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [
{
color: "#bdbdbd",
},
],
},
{
featureType: "administrative.neighborhood",
stylers: [
{
visibility: "off",
},
],
},
{
featureType: "poi",
elementType: "geometry",
stylers: [
{
color: "#eeeeee",
},
],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [
{
color: "#757575",
},
],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [
{
color: "#e5e5e5",
},
],
},
{
featureType: "poi.park",
elementType: "labels.text.fill",
stylers: [
{
color: "#9e9e9e",
},
],
},
{
featureType: "road",
elementType: "geometry",
stylers: [
{
color: "#ffffff",
},
],
},
{
featureType: "road.arterial",
elementType: "labels.text.fill",
stylers: [
{
color: "#757575",
},
],
},
{
featureType: "road.highway",
elementType: "geometry",
stylers: [
{
color: "#dadada",
},
],
},
{
featureType: "road.highway",
elementType: "labels.text.fill",
stylers: [
{
color: "#616161",
},
],
},
{
featureType: "road.local",
elementType: "labels.text.fill",
stylers: [
{
color: "#9e9e9e",
},
],
},
{
featureType: "transit.line",
elementType: "geometry",
stylers: [
{
color: "#e5e5e5",
},
],
},
{
featureType: "transit.station",
elementType: "geometry",
stylers: [
{
color: "#eeeeee",
},
],
},
{
featureType: "water",
elementType: "geometry",
stylers: [
{
color: "#c9c9c9",
},
],
},
{
featureType: "water",
elementType: "labels.text.fill",
stylers: [
{
color: "#9e9e9e",
},
],
},
];
function initMap() {
localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [
{ type: "bakery", weight: 1 },
{ type: "bank", weight: 1 },
{ type: "cafe", weight: 2 },
{ type: "department_store", weight: 1 },
{ type: "drugstore", weight: 1 },
{ type: "park", weight: 3 },
{ type: "restaurant", weight: 2 },
{ type: "primary_school", weight: 3 },
{ type: "secondary_school", weight: 3 },
{ type: "supermarket", weight: 2 },
],
maxPlaceCount: 24,
});
map = localContextMapView.map;
map.setOptions({
center: { lat: 51.507307, lng: -0.08114 },
zoom: 14,
styles,
});
// Build and add the Autocomplete search bar
const input = document.getElementById("input");
const options = {
types: ["address"],
componentRestrictions: {
country: "us",
},
fields: ["address_components", "geometry", "name"],
};
const autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.addListener("place_changed", () => {
const place = autocomplete.getPlace();
if (!place || !place.geometry) {
// User entered the name of a Place that was not suggested and
// pressed the Enter key, or the Place Details request failed.
window.alert("No address available for that input.");
return;
}
// Recenter the map to the selected address
map.setOptions({
center: place.geometry.location,
zoom: 14,
});
// Update the localContext directionsOptions origin
localContextMapView.directionsOptions = {
origin: place.geometry.location,
};
new google.maps.Marker({
position: place.geometry.location,
map: map,
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAQAAABKfvVzAAAAbUlEQVR4Ae3LoQ2AMAAF0TMYPJoV2IApGIJtmIMtmIAVqutraj6IiqZpmyYoCO/08R7bXbOOHSF2Ohr0HCh00EPdwImiTgYqRgxKMowUTFiUyTKRMeNQIcdMYsGjSp6FyIoaWkmoUuLxEPzDh1xIaLFFuTyHMgAAAABJRU5ErkJggg==",
zIndex: 30,
});
// update the results with new places
localContextMapView.search();
});
}
window.initMap = initMap;
참고: 자바스크립트는 TypeScript 스니펫에서 컴파일됩니다.
CSS
/*
* 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;
}
input {
height: 30px;
margin: 12px 0px;
padding: 10px;
width: 300px;
}
HTML
<html>
<head>
<title>Local Context Home</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
</head>
<body>
<input id="input" placeholder="Enter a US address" />
<div id="map"></div>
<!--
The `defer` attribute causes the callback to execute after the full HTML
document has been parsed. For non-blocking uses, avoiding race conditions,
and consistent behavior across browsers, consider loading using Promises
with https://www.npmjs.com/package/@googlemaps/js-api-loader.
-->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=localContext,places&v=beta"
defer
></script>
</body>
</html>