Google Maps API 是一组由 Google 提供的开发接口,允许开发者在自己的应用中集成地图、地点搜索、路线规划、街景等功能。以下是关于 Google Maps API 的详细解析:
Google Maps API 包含多个子服务,核心功能包括:
| API 类型 | 用途 | |------------------------|----------------------------------------------------------------------| | Maps JavaScript API | 网页端嵌入交互式地图,支持自定义标记、图层等。 | | Places API | 地点搜索、详情获取(如评分、营业时间)。 | | Geocoding API | 地址与经纬度的相互转换。 | | Directions API | 多模式路线规划(驾车、步行等)。 | | Distance Matrix API | 批量计算多起点到多终点的距离和耗时。 | | Street View API | 在网页或应用中嵌入街景图像。 |
<!DOCTYPE html>
<html>
<head>
<title>Google Maps API 示例</title>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
</head>
<body>
<div id="map" style="height: 500px; width: 100%;"></div>
<script>
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
center: { lat: 40.7128, lng: -74.0060 }, // 纽约坐标
zoom: 12,
});
// 添加标记点
new google.maps.Marker({
position: { lat: 40.7128, lng: -74.0060 },
map: map,
title: "纽约市",
});
// 地点搜索示例
const service = new google.maps.places.PlacesService(map);
service.nearbySearch(
{ location: { lat: 40.7128, lng: -74.0060 }, radius: 500, type: "restaurant" },
(results, status) => {
if (status === "OK") {
results.forEach(place => {
new google.maps.Marker({
position: place.geometry.location,
map: map,
});
});
}
}
);
}
window.initMap = initMap;
</script>
</body>
</html>
如需进一步功能(如自定义地图样式、实时数据更新),可参考 Google Maps 官方文档。
没有搜到相关的文章