0

我正在使用谷歌地图应用程序开发一个网页,但我遇到了一些麻烦。就目前而言,网页有一个功能地图(没有任何图层)和一个搜索栏。我是编程新手,所以希望有一个我缺少的快速修复。当我搜索地址时,地图上会放置一个地标。但是,地图不会放大地标。如何为每个搜索到的地址放大地图?

  <script type="text/javascript">
    var geocoder;
    var map; 
  function initialize() {
    geocoder = new google.maps.Geocoder ();
    var latlng = new google.maps.LatLng (55.1667, -114.4000);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
     map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
        }
    function codeAddress () {
        var address = document.getElementById ("address").value;
        geocoder.geocode ( { 'address': address}, function(results, status)  {
        if (status == google.maps.GeocoderStatus.OK)  {
            map.setCenter(results [0].geometry.location);
            var marker = new google.maps.Marker({
                map: map, 
                position: results [0].geometry.location
            });
            } 
        else {
            alert("Geocode was not successful for the following reason: " + status);
                }
    }); 
                            }
</script>

谢谢

4

1 回答 1

0

我会调用两个函数:

(1) 将地图置于标记的中心

要么您已经拥有 LatLng,要么通过marker.getPosition()调用map.setCenter(myLatLng).

(2) 设置地图的缩放

map.setZoom(zoomLevel), could be fixed around 12 to 16, or variable depending what kind of marker it is (street address or city overview?) This information can be stored in the marker variable, marker.zoomLevel.

function codeAddress () { 
  var address = document.getElementById ("address").value;
  geocoder.geocode ( {'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({ map: map, 
        position: results[0].geometry.location });
      map.setZoom(16);
    } 
    else { 
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
于 2012-05-28T19:39:57.377 回答