Reputation: 33954
Let's say I have a map inside a div
with width x
and height y
with t zoom level. How can I find the area, width and height of the currently visible map?
Upvotes: 1
Views: 3974
Reputation: 33954
Here is the answer,
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);
Upvotes: 9
Reputation: 13364
<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>
and ur define ur HTML components similar to...
<input id="searchTextField" type="text" size="50">
<div id="map_canvas"></div>
add following line line to header..
<script src="//maps.googleapis.com/maps/api/js?sensor=false&libraries=places"
type="text/javascript"></script>
EDIT You can get height and width from google.maps object.. I couldn't alter the code for your requirement. But I am sure this will give you a good idea.
Good Luck
Upvotes: -1