gpt4 book ai didi

javascript - 如何使用 Google Maps Javascript API 在多边形顶部添加标记?

转载 作者:行者123 更新时间:2023-11-28 17:15:32 25 4
gpt4 key购买 nike

我正在使用 Google Maps Javascript API,并且定义了一个多边形并将其附加到 map :

const region = new google.maps.Polygon({
map: map,
paths: [
{ lat: 40.5577151228437, lng: -74.15980859374997 },
{ lat: 40.599436503265856, lng: -74.27516503906247 },
{ lat: 40.67030312529891, lng: -74.25319238281247 },
{ lat: 40.72548139969253, lng: -74.26079167357881 },
],
});

我有一个点击处理程序,当用户点击时,它会在其上添加一个标记:

  google.maps.event.addListener(map, 'click', function (e) {
const marker = new google.maps.Marker({
map: map,
position: e.latLng,
});
});

当我单击多边形外部的区域时,我可以看到标记,但当我单击多边形内部时,我看不到标记。

我尝试过对每个 zIndex 进行硬编码,但没有成功。有什么想法吗?

最佳答案

两个选项:

  1. 向多边形添加点击监听器
google.maps.event.addListener(region, 'click', function (e) {
const marker = new google.maps.Marker({
map: map,
position: e.latLng,
});
});

proof of concept fiddle

function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
const region = new google.maps.Polygon({
map: map,
paths: [
{ lat: 40.5577151228437, lng: -74.15980859374997 },
{ lat: 40.599436503265856, lng: -74.27516503906247 },
{ lat: 40.67030312529891, lng: -74.25319238281247 },
{ lat: 40.72548139969253, lng: -74.26079167357881 },
],
});
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < region.getPath().getLength(); i++) {
bounds.extend(region.getPath().getAt(i));
}
map.fitBounds(bounds);

function addMarker(e) {
const marker = new google.maps.Marker({
map: map,
position: e.latLng,
});
}
google.maps.event.addListener(map, 'click', addMarker);
google.maps.event.addListener(region, 'click', addMarker);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

  • 使多边形不捕获点击(clickable: false);
  • const region = new google.maps.Polygon({
    map: map,
    clickable: false,
    paths: [
    { lat: 40.5577151228437, long: -74.15980859374997 },
    { lat: 40.599436503265856, long: -74.27516503906247 },
    { lat: 40.67030312529891, long: -74.25319238281247 },
    { lat: 40.72548139969253, long: -74.26079167357881 },
    ],
    }

    proof of concept fiddle

    function initialize() {
    var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
    center: new google.maps.LatLng(37.4419, -122.1419),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    const region = new google.maps.Polygon({
    map: map,
    clickable: false,
    paths: [
    { lat: 40.5577151228437, lng: -74.15980859374997 },
    { lat: 40.599436503265856, lng: -74.27516503906247 },
    { lat: 40.67030312529891, lng: -74.25319238281247 },
    { lat: 40.72548139969253, lng: -74.26079167357881 },
    ],
    });
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < region.getPath().getLength(); i++) {
    bounds.extend(region.getPath().getAt(i));
    }
    map.fitBounds(bounds);
    google.maps.event.addListener(map, 'click', function(e) {
    const marker = new google.maps.Marker({
    map: map,
    position: e.latLng,
    });
    });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
    height: 100%;
    width: 100%;
    margin: 0px;
    padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map_canvas"></div>

    关于javascript - 如何使用 Google Maps Javascript API 在多边形顶部添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53548026/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com