Google Maps API是一组允许开发者在自己的网站或应用中集成Google地图功能的接口。要显示特定国家或地区,主要使用Google Maps JavaScript API。
<!DOCTYPE html>
<html>
<head>
<title>显示特定国家/地区</title>
<style>
#map {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
// 创建地图实例,设置初始视图为中国
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 5,
center: { lat: 35.86166, lng: 104.195397 }, // 中国中心坐标
// 或者使用国家名称限制视图
restriction: {
latLngBounds: {
north: 53.5608,
south: 15.7753,
west: 73.5575,
east: 134.7739
},
strictBounds: true
}
});
}
</script>
<!-- 加载Google Maps API,替换YOUR_API_KEY -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
</body>
</html>
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 5
});
const geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': 'China' }, function(results, status) {
if (status === 'OK') {
map.setCenter(results[0].geometry.location);
// 设置视图边界
map.fitBounds(results[0].geometry.viewport);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
原因:坐标系统不匹配或使用了错误的投影 解决:确保使用WGS84坐标系统,检查坐标值是否正确
原因:API密钥无效或未启用相关服务 解决:
原因:网络问题或API调用过多 解决:
原因:未正确设置视图边界
解决:使用restriction
参数或fitBounds
方法明确设置边界
没有搜到相关的文章