【问题标题】:Add Marker function with Google Maps API使用 Google Maps API 添加标记功能
【发布时间】:2011-12-03 19:18:21
【问题描述】:

我有以下 Javascript,其中包括标准 Google Maps API initialize() 函数和自定义 addMarker() 函数。地图将正常加载,但标记不会添加到地图中。

<script type="text/javascript">

    // Standard google maps function
    function initialize() {
        var myLatlng = new google.maps.LatLng(40.779502, -73.967857);
        var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }

    // Function for adding a marker to the page.
    function addMarker(location) {
        marker = new google.maps.Marker({
            position: location,
            map: map
        });
    }

    // Testing the addMarker function
    CentralPark = new google.maps.LatLng(37.7699298, -122.4469157);
    addMarker(CentralPark);

</script>

【问题讨论】:

标签: javascript google-maps-api-3 google-maps-api-2


【解决方案1】:

您在函数外部添加了添加标记方法调用,这导致它在初始化方法之前执行,该方法将在谷歌地图脚本加载时调用,因此未添加标记,因为地图未初始化 执行以下操作...... 创建单独的方法 TestMarker 并从 initialize 调用它。

<script type="text/javascript">

    // Standard google maps function
    function initialize() {
        var myLatlng = new google.maps.LatLng(40.779502, -73.967857);
        var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        TestMarker();
    }

    // Function for adding a marker to the page.
    function addMarker(location) {
        marker = new google.maps.Marker({
            position: location,
            map: map
        });
    }

    // Testing the addMarker function
    function TestMarker() {
           CentralPark = new google.maps.LatLng(37.7699298, -122.4469157);
           addMarker(CentralPark);
    }
</script>

【讨论】:

    【解决方案2】:
    function initialize() {
        var location = new google.maps.LatLng(44.5403, -78.5463);
        var mapCanvas = document.getElementById('map_canvas');
        var map_options = {
          center: location,
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(map_canvas, map_options);
    
        new google.maps.Marker({
            position: location,
            map: map
        });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    

    【讨论】:

      【解决方案3】:

      这是另一种方法
      您还可以使用 setCenter 方法添加新标记

      检查下面的代码

      $('#my_map').gmap3({
            action: 'setCenter',
            map:{
               options:{
                zoom: 10
               }
            },
            marker:{
               values:
                [
                  {latLng:[position.coords.latitude, position.coords.longitude], data:"Netherlands !"}
                ]
            }
         });
      

      【讨论】:

        【解决方案4】:
        <div id="map" style="width:100%;height:500px"></div>
        
        <script>
        function myMap() {
          var myCenter = new google.maps.LatLng(51.508742,-0.120850);
          var mapCanvas = document.getElementById("map");
          var mapOptions = {center: myCenter, zoom: 5};
          var map = new google.maps.Map(mapCanvas, mapOptions);
          var marker = new google.maps.Marker({position:myCenter});
          marker.setMap(map);
        }
        </script>
        
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU&callback=myMap"></script>
        

        【讨论】:

          【解决方案5】:

          以下代码对我有用:

          <script src="http://maps.googleapis.com/maps/api/js"></script>
          <script>
              var myCenter = new google.maps.LatLng(51.528308, -0.3817765);
          
              function initialize() {
                     var mapProp = {
                      center:myCenter,
                      zoom:15,
                      mapTypeId:google.maps.MapTypeId.ROADMAP
                  };
                  var map = new google.maps.Map(document.getElementById("googleMap"), mapProp); 
          
                  var marker = new google.maps.Marker({
                      position: myCenter,
                      icon: {
                          url: '/images/marker.png',
                          size: new google.maps.Size(70, 86), //marker image size
                          origin: new google.maps.Point(0, 0), // marker origin
                          anchor: new google.maps.Point(35, 86) // X-axis value (35, half of marker width) and 86 is Y-axis value (height of the marker).
                      }
                  });
          
                  marker.setMap(map);
          
                  }
                  google.maps.event.addDomListener(window, 'load', initialize);
          
          </script>
          <body>
          <div id="googleMap" style="width:500px;height:380px;"></div>
          </body>
          

          Reference link

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-12-03
            • 1970-01-01
            • 2013-03-12
            • 2016-09-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多