How to add markers on top of a polygon using Google Maps Javascript API?

主宰稳场 提交于 2019-12-13 05:24:48

问题


I'm using the Google Maps Javascript API, and I've defined a polygon and attached it to a 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 },
  ],
});

I have a click handler that adds a marker on it when the user clicks:

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

I see the marker when I click on areas outside of the polygon, but I don't see the marker when I click inside the polygon.

I've tried hard-coding a zIndex on each, but no luck. Any ideas?


回答1:


Two options:

  1. add a click listener to the polygon
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>
  1. make the polygon not capture clicks (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>


来源:https://stackoverflow.com/questions/53548026/how-to-add-markers-on-top-of-a-polygon-using-google-maps-javascript-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!