【发布时间】:2020-11-15 05:33:17
【问题描述】:
我通过 @eocodezip 让 Google 地图与本地上下文 thanks to an answer 一起工作,但无法从弹出的附近位置启用路线。
这是我正在使用的代码。似乎“directionsOptions: { origin: center }”运行得太晚了,但我无法将其向上移动,因为直到稍后才定义“中心”。有没有办法更早地设置中心?至少我认为这是问题所在。
let map;
function initMap() {
const localContextMapView = new google.maps.localContext.LocalContextMapView({
element: document.getElementById("map"),
placeTypePreferences: ["restaurant", "tourist_attraction",
"bar", "cafe", "book_store", "convenience_store", "hospital"],
maxPlaceCount: 24,
});
map = localContextMapView.map;
let geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: "25325 Main St, Newhall, CA, USA" }, (results, status) => {
if (status === "OK") {
const center = results[0].geometry.location;
map.setCenter(center);
new google.maps.Marker({ position: center, map: map});
map.setOptions({
directionsOptions: { origin: center },
center: center,
zoom: 14,
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Local Context Basic</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=localContext&v=beta" defer></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
</body>
</html>
【问题讨论】:
标签: javascript google-maps google-maps-api-3 google-geocoder