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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| <!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] Déplacement d'un Marker</title>
<meta name="Author" content="NoSmoking">
<style type="text/css">
html, body {
height : 100%;
margin : 0;
padding : 0;
font-family : Verdana, Arial;
font-size : 1em;
}
h1 {
color : #4488bb;
font-size : 1.3em;
margin : 0;
padding : 0.5em;
border : 1px solid #4488bb;
}
#page {
padding : 0.5em;
}
#div_main {
margin : auto;
width : 800px;
}
#div_carte{
margin : auto;
margin-top : 1.0em;
width : 600px;
height: 600px;
border : 1px solid #c0c0c0;
}
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var tPosition = [
{ 'lat' :45.767299, 'lon' : 4.834329},
{ 'lat' :48.856667, 'lon' : 2.350987},
{ 'lat' :44.837368, 'lon' :-0.576144},
{ 'lat' :43.297612, 'lon' : 5.381042},
];
var nbPoint = tPosition.length;
var iPos = 0;
var i=0;
function movePositionMarker( marker, trajet){
// iPos %= nbPoint; // pas de débordement
// cretaion d'un nouveau marqueur
var oMarker = new google.maps.Marker({
'position' : marker.getPosition(), // position du marker en cours
'map': marker.getMap() // map du marker en cours
});
var cont='latitude :'+tPosition[i].lat+'<br>longitude :'+tPosition[i].lon+'<br>position :'+i+'';
// Encore quelques fonctions google maps pour ajouter un infobulle lors d'un clic sur un marqueur
google.maps.event.addListener(oMarker, 'click', function() {
var infowindow = new google.maps.InfoWindow({
content:cont,
size: new google.maps.Size(50,50),
});
//infowindow.open(map, marker);
infowindow.open(marker.getMap(), oMarker);
//infowindow.setContent('latitude :'+tPosition[iPos+1].lat+'<br>longitude :'+tPosition[iPos+1].lon+'');
});
iPos++; i=iPos-1; // incrémentation;
// recup. du point a traiter
// var point = new google.maps.LatLng( tPosition[iPos].lat, tPosition[iPos].lon);
var point1 = new google.maps.LatLng( tPosition[i].lat, tPosition[i].lon);
// deplace le marker
marker.setPosition( point1);
// ajout point dans polyline
trajet.getPath().push( point1);
// rappel de la fonction
setTimeout( function(){
movePositionMarker( marker, trajet);
}, 1000);
}
function initCarte(){
// init
var oMap, oMarker;
var mapOptions = {
backgroundColor : '#fff',
mapTypeControl : false,
streetViewControl : false,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
zoom : 6,
center : new google.maps.LatLng( 46.80, 1.75),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// creation de la carte
oMap = new google.maps.Map(document.getElementById("div_carte"), mapOptions);
// creation du marqueur
oMarker = new google.maps.Marker({
position : new google.maps.LatLng( tPosition[0].lat, tPosition[0].lon),
map : oMap
});
var oPolyline = new google.maps.Polyline({
'map': oMap
});
movePositionMarker( oMarker,oPolyline);
}
// init lorsque la page est chargee
google.maps.event.addDomListener(window, 'load', initCarte);
</script>
</head>
<body>
<div id="page">
<div id="div_main">
<h1>[Google Maps API V3] Déplacement d'un Marker</h1>
<div id="div_carte"></div>
</div>
</div>
</body>
</html> |
Partager