2012-03-02 117 views

回答

9

下面是答案,

var spherical = google.maps.geometry.spherical, 
    bounds = map.getBounds(), 
    cor1 = bounds.getNorthEast(), 
    cor2 = bounds.getSouthWest(), 
    cor3 = new google.maps.LatLng(cor2.lat(), cor1.lng()), 
    cor4 = new google.maps.LatLng(cor1.lat(), cor2.lng()), 
    width = spherical.computeDistanceBetween(cor1,cor3), 
    height = spherical.computeDistanceBetween(cor1, cor4); 
+0

我想說的是,當計算一條線上點之間的距離時,不需要幾何圖形庫......然後我記得我們生活在一個球體上。 #fezgame – Ziggy 2013-10-16 16:52:38

+1

好的答案 - 請注意,在初始化map/api時,您必須明確指定要使用幾何庫。 – Victor 2016-10-10 10:57:59

-1
<script type="text/javascript"> 
    function initialize() { 
    var mapOptions = { 
     center: new google.maps.LatLng(58, 60), 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), 
     mapOptions); 

    var input = document.getElementById('searchTextField'); 
    var autocomplete = new google.maps.places.Autocomplete(input); 

    autocomplete.bindTo('bounds', map); 

    var infowindow = new google.maps.InfoWindow(); 
    var marker = new google.maps.Marker({ 
     map: map 
    }); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     infowindow.close(); 
     var place = autocomplete.getPlace(); 
     if (place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
     } else { 
     map.setCenter(place.geometry.location); 
     map.setZoom(17); // Why 17? Because it looks good. 
     } 

     var image = new google.maps.MarkerImage(
      place.icon, 
      new google.maps.Size(71, 71), 
      new google.maps.Point(0, 0), 
      new google.maps.Point(17, 34), 
      new google.maps.Size(35, 35)); 
     marker.setIcon(image); 
     marker.setPosition(place.geometry.location); 

     var address = ''; 
     if (place.address_components) { 
     address = [(place.address_components[0] && 
        place.address_components[0].short_name || ''), 
        (place.address_components[1] && 
        place.address_components[1].short_name || ''), 
        (place.address_components[2] && 
        place.address_components[2].short_name || '') 
        ].join(' '); 
     } 

     infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address); 
     infowindow.open(map, marker); 
    }); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

和UR定義類似於烏爾HTML組件...

<input id="searchTextField" type="text" size="50"> <div id="map_canvas"></div>

添加下面一行行頭.. <script src="//maps.googleapis.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script>

編輯 你可以得到高度和寬度從google.maps對象..我無法改變你的要求的代碼。但我相信這會給你一個好主意。

好運

+1

你是從哪裏複製粘貼此。 – defau1t 2012-03-02 06:01:42

+0

寬度,高度,可用地圖面積在哪裏? – user960567 2012-03-02 06:01:47

相關問題