0

我创建了一个从 html 中的隐藏输入值中提取地址的地图,有人可以帮我在地址上显示一个标记吗?

<script>
      var map;
      var geocoder;
      var markers = new Array();
      var firstLoc;

      function myGeocodeFirst() {
        geocoder = new google.maps.Geocoder();

        geocoder.geocode( {'address': document.getElementById("text_address").value },
          function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              firstLoc = results[0].geometry.location;
              map = new google.maps.Map(document.getElementById("map_canvas"),
              {
                center: firstLoc,
                zoom: 17,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              });

            } 
            else {
              document.getElementById("text_status").value = status;
            }
          }
        );
      }  
window.onload=myGeocodeFirst;
</script>

<body>
<div id="loc">
<input id="text_address" type="hidden" size="10" value="Moshe Cohen 1, Bat Yam, Israel"></div>
<div id="map_canvas"></div>
</body>
</html>

谢谢,吉顿

4

1 回答 1

0

所以,我只是在我的脚本中添加了以下内容来添加标记——

<script>
      var map;
      var geocoder;
     // var markers = new Array();
      var firstLoc;


      function myGeocodeFirst() {
        geocoder = new google.maps.Geocoder();

        geocoder.geocode( {'address': document.getElementById("text_address").value },
          function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              firstLoc = results[0].geometry.location;
              map = new google.maps.Map(document.getElementById("map_canvas"),
              {
                center: firstLoc,
                zoom: 17,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              });
            var marker=new google.maps.Marker({
        position:firstLoc,
        });
        marker.setMap(map);
            } 
            else {
              document.getElementById("text_status").value = status;
            }
          }
        );

      }  


window.onload=myGeocodeFirst;
</script>
于 2013-03-24T14:18:05.790 回答