无法在Google地图上添加标记

时间:2014-04-24 15:34:25

标签: javascript google-maps

我尝试了很多方法,仍然无法将标记添加到谷歌地图。

要理解,请查看以下代码:

var marker;
    var myCenter = new google.maps.LatLng(55.1231231,-1.123131);

    function initialize()
    {
    var mapOption = {
      center: myCenter,
      zoom:15,
      mapTypeId:google.maps.MapTypeId.ROADMAP,
      panControl: true,
        panControlOptions: {
            position: google.maps.ControlPosition.RIGHT_TOP
        },
      };

    var map = new google.maps.Map(document.getElementById("googleMap"),mapOption);

    var customControlDiv = document.createElement('div');
        customControlDiv.id="customControlDiv";
        AddCustomControl(customControlDiv, map);
        map.controls[google.maps.ControlPosition.TOP_CENTER].push(customControlDiv);
    }

    function placeMarker(location) {
      var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title:"You are Here!"
      });
    }

    // to add the marker to the map
    marker.setMap(map);

    google.maps.event.addDomListener(window, 'load', initialize);

,HTML就在这里

<div id="googleMap"></div>

我没有忘记从谷歌apis打电话。它在这里

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>

所以,我已按照谷歌地图Here的说明进行操作。但它仍然无效。任何的想法?感谢。

1 个答案:

答案 0 :(得分:1)

的问题:

  • 您的地图是初始化函数的本地地址
  • 你的placeMarker函数被破坏了(你传入了位置,但是使用未定义的myLatLng作为位置)。
  • 你永远不会调用pl​​aceMarker函数
  • 未定义AddCustomControl

jsfiddle

    var marker;
    var myCenter = new google.maps.LatLng(55.1231231,-1.123131);

    function initialize()
    {
      var mapOption = {
        center: myCenter,
        zoom:15,
        mapTypeId:google.maps.MapTypeId.ROADMAP,
        panControl: true,
          panControlOptions: {
            position: google.maps.ControlPosition.RIGHT_TOP
          },
        };

      var map = new google.maps.Map(document.getElementById("googleMap"),mapOption);

      // to add the marker to the map
      var marker = new google.maps.Marker({
        position: myCenter,
        map: map,
        title:"You are Here!"
      });

    }

    google.maps.event.addDomListener(window, 'load', initialize);