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
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>[Google Maps API v3] Geocoding à la volée</title>
<style type="text/css">
html, body {
height : 100%;
}
#div_canvas {
margin : auto;
width : 600px;
height : 600px;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=fr"></script>
<script type="text/javascript">
var data =[
{ adresse : 'Bordeaux, FRANCE'},
{ adresse : 'Paris, FRANCE' },
{ adresse : 'Lille, FRANCE' },
{ adresse : 'Rennes, FRANCE' }
];
var geocoder;
var oMap;
var oBounds = new google.maps.LatLngBounds();
function initCarte() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(45, 3);
var myOptions = {
zoom : 8,
center : latlng = new google.maps.LatLng(45.0, 3.0),
backgroundColor : '#fff',
mapTypeId: google.maps.MapTypeId.ROADMAP
}
oMap = new google.maps.Map(document.getElementById("div_canvas"), myOptions);
for( var param=0; param < data.length; param++){
codeAddress( data[param].adresse);
}
}
function codeAddress(address){
geocoder.geocode( { 'address': address}, function(results, status) {
if( status == google.maps.GeocoderStatus.OK) {
oBounds.extend(results[0].geometry.location);
var marker = new google.maps.Marker({
map : oMap,
position: results[0].geometry.location
});
oMap.fitBounds( oBounds);
}
});
}
google.maps.event.addDomListener(window, 'load', initCarte);
</script>
</head>
<body>
<div id="div_canvas"></div>
</body>
</html> |
Partager