您可以将 LocalContextMapView
地点搜索的 locationRestriction
参数从受地图视口严格限制的默认设置更改为其他设置。在此示例中,我们将 locationRestriction
的边界设置为远大于地图的初始视口。尝试点击地点选择器中的地点,即可看到地图移动,以显示所选地点。
查看源代码
TypeScript
let map: google.maps.Map;
function initMap() {
const center = { lat: 37.4219998, lng: -122.0840572 };
const bigBounds = {
north: 37.432,
south: 37.412,
west: -122.094,
east: -122.074,
};
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [{ type: "restaurant" }],
maxPlaceCount: 12,
locationRestriction: bigBounds,
directionsOptions: { origin: center },
});
map = localContextMapView.map!;
new google.maps.Marker({ position: center, map: map });
map.setOptions({
center: center,
zoom: 16,
});
}
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
注意 :请参阅 TypeScript 和 Google 地图的使用方法指南 。
JavaScript
let map;
function initMap() {
const center = { lat: 37.4219998, lng: -122.0840572 };
const bigBounds = {
north: 37.432,
south: 37.412,
west: -122.094,
east: -122.074,
};
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [{ type: "restaurant" }],
maxPlaceCount: 12,
locationRestriction: bigBounds,
directionsOptions: { origin: center },
});
map = localContextMapView.map;
new google.maps.Marker({ position: center, map: map });
map.setOptions({
center: center,
zoom: 16,
});
}
window.initMap = initMap;
注意 :所示 JavaScript 代码是从 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;
}
HTML
<html>
<head>
<title>Local Context Restrictions</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>
<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.
See https://developers.google.com/maps/documentation/javascript/load-maps-js-api
for more information.
-->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=localContext&v=beta"
defer
></script>
</body>
</html>
试用示例
了解代码
指定 Local Context 搜索边界
利用 LatLngBounds
属性设定 locationRestriction
严格搜索边界。此示例使用了 LatLngBoundsLiteral
接口来创建 LatLngBounds
对象。
TypeScript
const bigBounds = {
north: 37.432,
south: 37.412,
west: -122.094,
east: -122.074,
};
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [{ type: "restaurant" }],
maxPlaceCount: 12,
locationRestriction: bigBounds,
directionsOptions: { origin: center },
});
注意 :请参阅 TypeScript 和 Google 地图的使用方法指南 。
JavaScript
const bigBounds = {
north: 37.432,
south: 37.412,
west: -122.094,
east: -122.074,
};
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [{ type: "restaurant" }],
maxPlaceCount: 12,
locationRestriction: bigBounds,
directionsOptions: { origin: center },
});
Local Context 地图视口与 directionsOptions
之间的关系
如果所选地图注点 (POI) 在当前视口之外,Local Context 地图视图会自动移动,以在可见边界内显示该 POI,而无需任何代码来管理地图。
如果您没有为 LocalContextMapView
的 directionsOptions
属性指定起点,地图将会移动,以仅显示所选的 POI。
如果您为 directionsOptions
指定了起点,地图将同时显示起点和所选的 POI,以及这两点之间的步行路线。
TypeScript
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [{ type: "restaurant" }],
maxPlaceCount: 12,
locationRestriction: bigBounds,
directionsOptions: { origin: center },
});
注意 :请参阅 TypeScript 和 Google 地图的使用方法指南 。
JavaScript
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: [{ type: "restaurant" }],
maxPlaceCount: 12,
locationRestriction: bigBounds,
directionsOptions: { origin: center },
});
试用示例