首先,你需要去 Google Cloud Console 申请一个 API key。然后在你的 HTML 中引入 Google Maps JavaScript API,例如:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
async defer></script>
接下来,你可以使用 JavaScript 代码来调用 Google Maps API。例如,你可以使用如下代码来获取店铺名为 "Starbucks" 的经纬度:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
var geocoder = new google.maps.Geocoder();
var address = "Starbucks";
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
console.log(lat, lng);
// 你可以在这里使用经纬度
} else {
console.log('Geocode was not successful for the following reason: ' + status);
}
});
}
希望这能帮到你!