谷歌地图标记API v3无法显示

13

标记未显示,我阅读了文档但找不到问题,有人可以帮我吗?

以下是js代码:

function initialize() {
      var mapOptions = {
        center: new google.maps.LatLng(-8.064903, -34.896872),
        zoom: 16,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

      var marker = new google.maps.Marker({
          position: location,
          title:"Hello World!",
          visible: true
      });
      marker.setMap(map);
    }
1个回答

38
我的猜测是你的“location”对象未被定义。尝试将标记位置设置为与地图中心相同的LatLng,看看是否有效:
function initialize() {
  latLng = new google.maps.LatLng(-8.064903, -34.896872)
  var mapOptions = {
    center: latLng,
    zoom: 16,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

  var marker = new google.maps.Marker({
      position: latLng,
      title:"Hello World!",
      visible: true
  });
  marker.setMap(map);
}

我刚刚创建了变量location = new google.maps.LatLng(-8.064903,-34.896872);它起作用了!谢谢! - Ramon Vasconcelos
1
@RamonVasconcelos 太好了!如果答案解决了您的问题,请不要忘记接受该答案,并祝您在 GMaps 中好运! - alestanis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接