您可以使用Maps JavaScript API的地理編碼器服務來解析輸入(例如巴黎)來協調和居中地圖。
看一看下面的代碼示例
var map;
function initMap() {
var geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 8
});
geocoder.geocode({'address': "Paris"}, function(results, status) {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
} 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;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initMap" async defer></script>
您可以使用[谷歌地圖API的地理編碼(https://developers.google.com/maps/documentation/geocoding /開始)獲取所需城市的緯度/經度 –