【发布时间】:2018-08-14 09:05:41
【问题描述】:
我正在努力实现以下目标
主页:访问者将从搜索字段中搜索城市或位置。根据谷歌文档的缩放级别如下。
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings
在搜索页面上:我想列出搜索到的位置中的所有属性,例如加利福尼亚所以我想用 Zoom level 10 在地图上显示所有属性.. 但问题是我正在努力寻找城市的坐标.. 做我必须在数据库中添加所有城市坐标?见下面的代码..我怎样才能使center: {lat: 19.1760, lng: 72.7954}动态所以它会相应地改变。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/* 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;
}
</style>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBkyMvhWRirAMPvBnjgzXEH6DIkjwXwW_A&callback=initMap">
</script>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: 19.1760, lng: 72.7954}
});
setMarkers(map);
}
var beaches = [
['Aksa Beach', 19.1760, 72.7954, 1],
['Juhu Beach', 19.0969, 72.8266, 1]
];
function setMarkers(map) {
var shape = {
coords: [1, 1, 1, 20, 18, 20, 18, 1],
type: 'poly'
};
for (var i = 0; i < beaches.length; i++) {
var beach = beaches[i];
var marker = new google.maps.Marker({
position: {lat: beach[1], lng: beach[2]},
map: map,
shape: shape,
title: beach[0],
zIndex: beach[3]
});
marker.addListener('click', function() {
map.setZoom(20);
map.setCenter(marker.getPosition());
});
}
}
</script>
</body>
</html>
简而言之,我怎样才能动态获取城市/大陆/街道等的坐标来渲染地图?因为坐标是必需的属性。
【问题讨论】:
标签: google-maps google-maps-api-3 google-maps-markers