この例では、ユーザーが住所を入力して付近の設備を評価できるように、住所用の Place Autocomplete 検索バーを追加しています。住宅街のユーザーの場合、placeTypePreferences
は、住宅での生活に関連する一般的なタイプに設定されます。このサンプルの住所は米国に限定されています。
ソースコードを表示
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 = <HTMLInputElement>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(); }); }
JavaScript
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(); }); }
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
<!DOCTYPE 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 src="./index.js"></script> </head> <body> <input id="input" placeholder="Enter a US address" /> <div id="map"></div> <!-- Async script executes immediately and must be after any DOM elements used in callback. --> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=localContext,places&v=beta" async ></script> </body> </html>
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) {
// ユーザーが推奨されていない場所の名前を入力して Enter キーを押した場合、
// または Place Details リクエストが失敗した場合。
window.alert("No address available for that input.");
return;
}
// 選択された住所に地図の中心を設定します
map.setOptions({
center: place.geometry.location,
zoom: 14,
});
// localContext directionsOptions の出発地を更新します
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,
});
// 結果を新しい場所の情報に更新します
localContextMapView.search();
});
}
/* 常に地図の高さを明示的に設定して、地図を含む div * 要素の
サイズを定義します。*/
#map {
height: 100%;
}
/* 省略可: サンプルページをウィンドウ全体に表示します。*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
input {
height: 30px;
margin: 12px 0px;
padding: 10px;
width: 300px;
}
<!DOCTYPE html>
<html>
<head>
<title>Local Context Home</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<input id="input" placeholder="Enter a US address" />
<div id="map"></div>
<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=localContext,places&v=beta&channel=2"
async
></script>
</body>
</html>
サンプルを試す
コードを理解する
場所のタイプの表示頻度を調整する
ローカル コンテキスト ライブラリが返す場所のタイプを指定する際は、weight
属性を使用して、各プロパティに相対的な重み付けを割り当てることができます(相対的な重み付けが高いタイプは表示頻度が高くなります)。この例では、公園や学校がある場合に、それらの表示頻度が高くなるよう指定しています。
TypeScript
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 }, ],
JavaScript
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 }, ],
Place Autocomplete
Place Autocomplete サービスを使用するには、まずプレイス ライブラリと Maps JavaScript API を読み込む必要があります。Autocomplete
のドキュメントでは、地域や場所のタイプに応じた予測のフィルタリングなど、プレイス オートコンプリートの動作のカスタマイズに使用できるすべてのパラメータについて詳しく説明しています。
- プロジェクトで Places API を有効にします。
- ローカル コンテキスト ライブラリに加えて、プレイス ライブラリ、JavaScript API を読み込みます。
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=localContext,places&v=beta&callback=initMap">
</script>
- Place Autocomplete 検索バーをウェブサイトや地図に追加する方法については、
Autocomplete
のドキュメントやコードサンプルをご覧ください。この例では、関連する行は次のようになります。
TypeScript
// Build and add the Autocomplete search bar const input = <HTMLInputElement>document.getElementById("input"); const options = { types: ["address"], componentRestrictions: { country: "us", }, fields: ["address_components", "geometry", "name"], }; const autocomplete = new google.maps.places.Autocomplete(input, options);
JavaScript
// 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);