似乎无法在我的地图上添加可点击的标记

时间:2019-01-26 18:14:22

标签: javascript api google-maps

我试图按照Google文档中的说明添加可点击的标记,但是它对我不起作用,所以我尝试了一些youtube视频,但仍然不起作用!

我的代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      #map {
        height:100%;
        width:100%;
      }
      html, body {
        height: 100vh;
        width:100%;
        margin: 0;
        padding: 0;
      }
    </style>
    <script>
      var map;

      function initMap() {
        map = new google.maps.Map( document.getElementById('map'), {
          center: {lat: 38.736946, lng:  -9.142685},
          zoom: 5,
        });

        var iconLook = {
          url: "look.png",
          scaledSize: new google.maps.Size(50, 50),
          origin: new google.maps.Point(0,0),
          anchor: new google.maps.Point(25, 25)
        };
        var iconCritic = {
          url: "critic.png",
          scaledSize: new google.maps.Size(50, 50),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(25, 25)
        };

        var LisbonMessage = "Vendas - 100/H";
        var PortoMessage = "Vendas - 10/H";

        var MarkerLisbon = new google.maps.Marker({
          position: {lat: 38.70638772028284, lng:  -9.13080236673386},
          map:map,
          title: LisbonMessage,
          icon: iconLook
        });

        var MarkerPorto = new google.maps.Marker({
          position: {lat: 41.16207496560443, lng:  -8.628419824576099},
          map:map,
          title: PortoMessage,
          icon: iconLook
        });
    }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap" async defer></script>
  </head>
  <body>
    <div id="map"></div>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

在为click事件创建侦听器之前,所有内容都无法点击。

来自the docs

 marker.addListener('click', function() {
    map.setZoom(8);
    map.setCenter(marker.getPosition());
    });

答案 1 :(得分:0)

请参见documentation example for a simple click listener

假设您要在点击时打开InfoWindow,您将执行以下操作:

var infowindow = new google.maps.InfoWindow();
MarkerLisbon.addListener('click', function(evt) {
  infowindow.setContent(LisbonMessage);
  infowindow.open(map, this);
})

proof of concept fiddle

screenshot of resulting map

代码段:

var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 38.736946,
      lng: -9.142685
    },
    zoom: 5,
  });
  var infowindow = new google.maps.InfoWindow();

  var LisbonMessage = "Vendas - 100/H";
  var PortoMessage = "Vendas - 10/H";


  var MarkerLisbon = new google.maps.Marker({
    position: {
      lat: 38.70638772028284,
      lng: -9.13080236673386
    },
    map: map,
    title: LisbonMessage,
  });
  MarkerLisbon.addListener('click', function(evt) {
    infowindow.setContent(LisbonMessage);
    infowindow.open(map, this);
  })


  var MarkerPorto = new google.maps.Marker({
    position: {
      lat: 41.16207496560443,
      lng: -8.628419824576099
    },
    map: map,
    title: PortoMessage,
  });
  MarkerPorto.addListener('click', function(evt) {
    infowindow.setContent(PortoMessage);
    infowindow.open(map, this);
  })
}
#map,
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>