固定谷歌地图上的标记

时间:2013-10-25 16:20:47

标签: javascript jquery css google-maps google-maps-api-3

我正在尝试复制this JSFIDDLE link中显示的固定标记功能,但是尽管使用了相同的代码,但标记似乎不会在我的html页面中呈现。除了js小提琴代码之外,我唯一添加的是脚本源。有人可以建议我哪里出错吗?

以下是我从JSFIDDLE link复制的代码。

    <!DOCTYPE html>

<html>
<head>
    <title>Page Title</title>
    <style>
        body,html,#map_canvas{height:100%;margin:0;}
#map_canvas .centerMarker{
  position:absolute;
  /*url of the marker*/
  background:url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;
  /*center the marker*/
  top:50%;left:50%;
  z-index:1;
  /*fix offset when needed*/
  margin-left:-10px;
  margin-top:-34px;
  /*size of the image*/
  height:34px;
  width:20px;
  cursor:pointer;
}
    </style>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>


    <script>
            function initialize() {
        var mapOptions = {
          zoom: 14,
          center: new google.maps.LatLng(52.5498783, 13.425209099999961),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
        $('<div/>').addClass('centerMarker').appendTo(map.getDiv())
             //do something onclick
            .click(function(){
               var that=$(this);
               if(!that.data('win')){
                that.data('win',new google.maps.InfoWindow({content:'this is the center'}));
                that.data('win').bindTo('position',map,'center');
               }
               that.data('win').open(map);
            });
      };

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

    </script>

</head>

<body>
<div id="map_canvas"></div>


</body>
</html>

2 个答案:

答案 0 :(得分:1)

您需要包含jQuery,因为您正在使用$('<div/>') ..

我运行了你的文件,添加

后工作正常
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

请告诉我这是否适合您:)

P.S。您可能错过了jQuery,因为它包含在jsFiddle左侧的下拉列表中。

答案 1 :(得分:0)

我注意到上面代码中的jQuery脚本源中缺少http:,添加它解决了我的问题。

这是更新的代码。

<!DOCTYPE html>

<html>
<head>
    <title>Page Title</title>
    <meta charset="UTF-8"> 
    <style>
    body,html,#map_canvas{height:100%;margin:0;}
    #map_canvas .centerMarker{
    position:absolute;
    /*url of the marker*/
    background:url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;
    /*center the marker*/
    top:50%;left:50%;
    z-index:1;
    /*fix offset when needed*/
    margin-left:-10px;
    margin-top:-34px;
    /*size of the image*/
    height:34px;
    width:20px;
    cursor:pointer;
    }
    </style>


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


    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&.js"></script>


    <script>
        function initialize() {
        var mapOptions = {
          zoom: 14,
          center: new google.maps.LatLng(52.5498783, 13.425209099999961),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
        $('<div/>').addClass('centerMarker').appendTo(map.getDiv())
             //do something onclick
            .click(function(){
               var that=$(this);
               if(!that.data('win')){
                that.data('win',new google.maps.InfoWindow({content:'this is the center'}));
                that.data('win').bindTo('position',map,'center');
               }
               that.data('win').open(map);
            });
      };

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

    </script>

</head>

<body>
<div id="map_canvas"></div>


</body>
</html>