简体   繁体   English

我正在尝试使用Google Maps Javascript API添加标记,但它无法正常工作

[英]I'm trying to add a marker using Google Maps Javascript API but it isn't working

This is my Javascript code to obtain the marker. 这是我获取标记的Javascript代码。 The page is loading the map but the marker doesn't appear. 页面正在加载地图,但标记未显示。

function initMap(){
      var options = {
        zoom:13,
        center:{lat:42.3601,lng:-71.0589}//Change these coordinates to change the center
      }
      var map = new google.maps.Map(document.getElementById('map'),options);
    }
    var location = new google.maps.LatLng(42.3601, -71.0589);
    var marker = new google.maps.Marker({
      position:location,
      map:this
    });
    marker.setMap(map);

Two issues: 两个问题:

  1. create the marker inside your initMap function (which most likely is the callback function that runs once the API is loaded) initMap函数中创建marker (很可能是加载API后运行的回调函数)

  2. use a valid value for the google.maps.Map object. 使用google.maps.Map对象的有效值。 (the map variable inside the initMap function would be best, this where you have it is probably the window object). (在map里面变量initMap功能将是最好的, this ,你有这大概是window对象)。

function initMap(){
  var options = {
    zoom:13,
    center:{lat:42.3601,lng:-71.0589}//Change these coordinates to change the center
  }
  var map = new google.maps.Map(document.getElementById('map'),options);
  var location = new google.maps.LatLng(42.3601, -71.0589);
  var marker = new google.maps.Marker({
    position:location,
    map:map
  });
}

proof of concept fiddle 概念证明小提琴

结果地图的屏幕截图

code snippet: 代码段:

 function initMap() { var options = { zoom: 13, center: { lat: 42.3601, lng: -71.0589 } //Change these coordinates to change the center } var map = new google.maps.Map(document.getElementById('map'), options); var location = new google.maps.LatLng(42.3601, -71.0589); var marker = new google.maps.Marker({ position: location, map: map }); } 
 html, body, #map { height: 100%; margin: 0; padding: 0; } 
 <div id="map"></div> <!-- Replace the value of the key parameter with your own API key. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap" async defer></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript Google Maps v3 API-无法添加标记 - Javascript Google Maps v3 API - can't add marker Google maps API - 按地址javascript添加标记 - Google maps API - add marker by address javascript 无法在Google Maps Api上添加标记 - Can't add Marker on Google maps Api Google Maps API-为什么不起作用? - Google Maps API — Why isn't this working? 如何使用google maps v3检查标记是否在边界内 - How can I check the marker is or isn't in the bounds using google maps v3 使用javascript无法使用Google Maps API添加/删除折线 - Add/Remove polylines using Google Maps API using javascript not working 使用 Google 地图 Javascript API 添加标记,使其看起来与在 maps.google.com 中添加的标记完全相同 - Add marker with Google Maps Javascript API to look exactly as marker that were added in maps.google.com 我正在尝试将标记集群添加到我的谷歌地图脚本 - I am trying to add marker clusters to my google maps script 我正在尝试从我的电子表格中获取图表以使用谷歌脚本发送 email,但 getAs() function 不起作用 - I'm trying to get a chart from my spreadsheet to send in an email using google script, but the getAs() function isn't working 在StageWebView的AIR Android项目中使用的JavaScript JavaScript Google Maps API无法正常工作吗? - Google Maps API for JavaScript isn't working properly when used in an AIR Android Project within StageWebView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM