【问题标题】:show a marker on a Google maps map在 Google 地图上显示标记
【发布时间】:2013-12-13 22:03:39
【问题描述】:

我构建了这个脚本来初始化特定点的地图:

注意:(x, y) 是经纬度坐标。

<script type="text/javascript">
        var businessMap;

        function showBusinessMap(x, y) {
            var xops = 
            {
                zoom: 15,
                center: new google.maps.LatLng(x, y)
            }

            var chosenPoint = new google.maps.Marker({
                position: new google.maps.LatLng(x, y),
            });

            businessMap = new google.maps.Map(document.getElementById("businessMap"), xops);

            chosenPoint.setMap(businessMap);
            chosenPoint.setVisible(true);


        }
</script>

效果很好,但地图上没有显示标记。

我也有这个调整地图大小的 jQuery 函数:

            $('#showMap').on('shown', function() {
                google.maps.event.trigger(businessMap, "resize");
            });

如何使标记chosenPoint 显示?

如果我遗漏了任何信息,请告诉我,我会添加。

提前致谢!

编辑:

    var businessMap;

    function showBusinessMap(x, y) {
        var xops = 
        {
            zoom: 15,
            center: new google.maps.LatLng(x, y)
        }

        businessMap = new google.maps.Map(document.getElementById("businessMap"), xops);

        var chosenPoint = new google.maps.Marker({
            position: new google.maps.LatLng(x, y),
            map: businessMap
        });
    }

还是不行。

【问题讨论】:

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


【解决方案1】:
 var address = "Address, City, ProvinceOrState, Country"; 

 geocoder.geocode({ 'address': address }, function (results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
         //console.log("GeocoderStatus.OK  :  " + results[0].geometry.location);
         var mapOptions = {
             zoom: 8,
             center: results[0].geometry.location,
             mapTypeId: google.maps.MapTypeId.ROADMAP
         }
         //load the map
         map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

         var marker = new google.maps.Marker({
             map: map,
             position: results[0].geometry.location
         });

     } //if
     else { console.log("Geocode was not successful for the following reason: " + status); }
 })

以下部分是添加“标记”的部分

         var marker = new google.maps.Marker({
             map: map,
             position: results[0].geometry.location
         });

【讨论】:

    【解决方案2】:

    编辑: 您需要将您创建的chosenPoint 添加到地图中。

    来自 Google Maps API,

    var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
    var mapOptions = {
      zoom: 4,
      center: myLatlng
    }
    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    
    // To add the marker to the map, use the 'map' property
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title:"Hello World!"
    });
    

    因此,在您的情况下,首先创建地图对象,然后创建标记对象,并在标记的选项中包含 map: businessMap,就像上面的示例一样。

    Look here for more information.

    【讨论】:

      【解决方案3】:

      作为起点,这里有一个基于您的代码的简单示例。我想您的问题是由您向我们展示的内容之外的东西引起的。

      <script type="text/javascript">
      
              var businessMap;
      
              function showBusinessMap(x, y) {
      
                  var xops = {
                      zoom: 15,
                      center: new google.maps.LatLng(x, y)
                  };
                  var businessMap = new google.maps.Map(document.getElementById("businessMap"),
                  xops);
      
                  var chosenPoint = new google.maps.Marker({
                      position: new google.maps.LatLng(x, y),
                      map: businessMap
                  });
      
              }
      
              function initialize() {
                  showBusinessMap(-34.397, 150.644);
              }
      
              google.maps.event.addDomListener(window, 'load', initialize);
      
      </script>
      

      【讨论】:

        猜你喜欢
        • 2021-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        • 2016-11-09
        • 2011-08-07
        • 2019-01-05
        相关资源
        最近更新 更多