1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Limit Panning</title>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script>
<SCRIPT type=text/javascript
src="http://www.google.com/jsapi?key=ABQIAAAAagFDi9TA8BHTFjb4GZKsEhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRor_cJ2gz59NOCUMH8Jc-ChI8D2A"></SCRIPT>
</head>
<body>
<div>
<input id="address" type="textbox" value="82 avenue Charles de Gaulle Neuilly">
<input type="button" value="Encode" onclick="codeAddress()">
</div>
<div id="map" style="width: 100%; height: 400px;"></div>
<script type="text/javascript">
var minZoomLevel = 14;
var geoCodeur;
geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('map'), {
zoom: minZoomLevel,
maxZoom:20,
minZoom:14,
scaleControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
center: new google.maps.LatLng(48.88421032414912, 2.2668671485351544),
mapTypeId: google.maps.MapTypeId.HYBRID
});
// Bounds pour Neuilly
var allowedBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(48.87368300861551, 2.242276656286619),
new google.maps.LatLng(48.899757345074015, 2.288968550817869));
// Dragend event
google.maps.event.addListener(map, 'dragend', function() {
if (allowedBounds.contains(map.getCenter())) return;
// Out of bounds - remet la map dans les limites
var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = allowedBounds.getNorthEast().lng(),
maxY = allowedBounds.getNorthEast().lat(),
minX = allowedBounds.getSouthWest().lng(),
minY = allowedBounds.getSouthWest().lat();
if (x < minX) x = minX;
if (x > maxX) x = maxX;
if (y < minY) y = minY;
if (y > maxY) y = maxY;
map.setCenter(new google.maps.LatLng(y, x));
});
// Limit the zoom level
google.maps.event.addListener(map, 'zoom_changed', function() {
if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});
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>
</body>
</html> |
Partager