调整 Local Context 搜索边界

您可以将 LocalContextMapView 地点搜索的 locationRestriction 参数从受地图视口严格限制的默认设置更改为其他设置。在此示例中,我们将 locationRestriction 的边界设置为远大于地图的初始视口。尝试点击地点选择器中的地点,即可看到地图移动,以显示所选地点。

了解代码

指定 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 },
});

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,而无需任何代码来管理地图。

如果您没有为 LocalContextMapViewdirectionsOptions 属性指定起点,地图将会移动,以仅显示所选的 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 },
});

JavaScript

const localContextMapView = new google.maps.localContext.LocalContextMapView({
  element: document.getElementById("map"),
  placeTypePreferences: [{ type: "restaurant" }],
  maxPlaceCount: 12,
  locationRestriction: bigBounds,
  directionsOptions: { origin: center },
});

试用示例