"感兴趣的区域"(Area of Interest, AOI)在地图应用中是指用户或开发者特别关注的地理区域,通常需要在地图上突出显示或进行特殊处理。
// 初始化地图
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: { lat: 39.9042, lng: 116.4074 }, // 北京中心坐标
});
// 定义感兴趣区域的边界坐标
const aoiCoords = [
{ lat: 39.91, lng: 116.39 },
{ lat: 39.91, lng: 116.42 },
{ lat: 39.89, lng: 116.42 },
{ lat: 39.89, lng: 116.39 },
];
// 创建多边形
const aoiPolygon = new google.maps.Polygon({
paths: aoiCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
editable: false,
map: map,
});
}
function showAOIWithRectangle() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: { lat: 39.9042, lng: 116.4074 },
});
const aoiBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(39.89, 116.39), // 西南角
new google.maps.LatLng(39.91, 116.42) // 东北角
);
const aoiRectangle = new google.maps.Rectangle({
strokeColor: "#0000FF",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#0000FF",
fillOpacity: 0.35,
bounds: aoiBounds,
map: map,
});
}
function showAOIWithCircle() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: { lat: 39.9042, lng: 116.4074 },
});
const aoiCircle = new google.maps.Circle({
strokeColor: "#00FF00",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#00FF00",
fillOpacity: 0.35,
map: map,
center: { lat: 39.9042, lng: 116.4074 },
radius: 1000, // 半径,单位米
});
}
function addInfoWindowToAOI() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: { lat: 39.9042, lng: 116.4074 },
});
const aoiCoords = [
{ lat: 39.91, lng: 116.39 },
{ lat: 39.91, lng: 116.42 },
{ lat: 39.89, lng: 116.42 },
{ lat: 39.89, lng: 116.39 },
];
const aoiPolygon = new google.maps.Polygon({
paths: aoiCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
});
const infoWindow = new google.maps.InfoWindow({
content: "<h3>感兴趣的区域</h3><p>这里是重点监控区域</p>",
});
aoiPolygon.addListener("click", (event) => {
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
});
}
function calculateAOIArea() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: { lat: 39.9042, lng: 116.4074 },
});
const aoiCoords = [
{ lat: 39.91, lng: 116.39 },
{ lat: 39.91, lng: 116.42 },
{ lat: 39.89, lng: 116.42 },
{ lat: 39.89, lng: 116.39 },
];
const aoiPolygon = new google.maps.Polygon({
paths: aoiCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
});
// 计算面积(平方米)
const area = google.maps.geometry.spherical.computeArea(aoiPolygon.getPath());
console.log(`AOI面积: ${area.toFixed(2)} 平方米`);
}
原因:
解决方案:
// 确保坐标点形成闭合区域
const correctCoords = [
{ lat: 39.91, lng: 116.39 },
{ lat: 39.91, lng: 116.42 },
{ lat: 39.89, lng: 116.42 },
{ lat: 39.89, lng: 116.39 },
{ lat: 39.91, lng: 116.39 } // 闭合多边形
];
原因:
fillOpacity
设置为0解决方案:
const aoiPolygon = new google.maps.Polygon({
paths: aoiCoords,
fillColor: "#FF0000",
fillOpacity: 0.35, // 确保大于0
map: map,
});
优化方案:
MarkerClusterer
聚合标记google.maps.Data
类处理大量地理数据通过以上方法,您可以在Google Maps API上有效地显示和管理感兴趣的区域。
没有搜到相关的文章