Con este código pinto el mapa:
Código Javascript
:
Ver originalvar map;
function initialize() {
var latlng = new google.maps.LatLng(15.623037,-90.351562);
var settings = {
zoom: 4,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), settings);
setInterval("unidades()",2000);
//unidades()
}
google.maps.event.addDomListener(window, 'load', initialize);
con setInterval llamó cada 2 segundos a la funcion unidades que es la que me muestra la ubicación del vehículo así como su trayectoria recorrida. Te la comparto, es esta:
Código Javascript
:
Ver originalfunction unidades(){
<?Php
$imei_sql="EXEC sp_GetIMEI";
$res_imei=mssql_query($imei_sql);
while($array_imei=mssql_fetch_array($res_imei)){
$imei=$array_imei['IMEI'];
$sqlquery="EXEC sp_GetLastRecord '$imei'";
$res_sqlquery=mssql_query($sqlquery);
$ar_sqlquery=mssql_fetch_array($res_sqlquery);
//$latitud=$ar_query['LATITUDE'];
//$longitud=$ar_query['LONGITUDE'];
//$imei=$ar_query['IMEI'];
//$speed=$ar_query['SPEED'];
?>
var contentString = '<div class="content">'+
'<div class="siteNotice">'+
'</div>'+
'<h3 class="firstHeading"><?Php echo $array_imei['NAME']; ?></h3>'+
'<div class="bodyContent">'+
'<p><?Php echo $ar_sqlquery['IMEI']; ?></p>'+
'<p>Latitude: <?Php echo $ar_sqlquery['LATITUDE']; ?></p>'+
'<p>Longitud: <?Php echo $ar_sqlquery['LONGITUDE']; ?></p>'+
'<p>Velocidad: <?Php echo $ar_sqlquery['SPEED']; ?> Km/h</p>'+
'<p><?Php echo $ar_sqlquery['TIMESTAMP']; ?></p>'+
'<p><a target="_blank" href="http://maps.google.com/maps?f=q&q=<?Php echo $ar_sqlquery['LATITUDE']; ?>,<?Php echo $ar_sqlquery['LONGITUDE']; ?>&z=16">Ver en Google maps</a></p>'+
'</div>'+
'</div>';
var infowindow<?Php echo $cont; ?> = new google.maps.InfoWindow({
content: contentString
});
/*Car*/
var carLogo = new google.maps.MarkerImage('images/car.png',
new google.maps.Size(40,40),
new google.maps.Point(0,0),
new google.maps.Point(20,20)
);
var carPos = new google.maps.LatLng(<?Php echo $ar_sqlquery['LATITUDE']; ?>,<?Php echo $ar_sqlquery['LONGITUDE']; ?>);
var carMarker<?Php echo $cont; ?> = new google.maps.Marker({
position: carPos,
map: map,
icon: carLogo,
title:"<?Php echo $array_imei['NAME']; ?>"
});
/*Muestra globo con la info*/
google.maps.event.addListener(carMarker<?Php echo $cont; ?>, 'click', function() {
infowindow<?Php echo $cont; ?>.open(map,carMarker<?Php echo $cont; ?>);
});
/*Polilínea*/
var Ruta = [
<?Php
$sql="EXEC sp_GetLastHundred '$imei'";
$res_sql=mssql_query($sql);
//$ar_sql=mssql_fetch_array($res_sql)
while ($ar_sql=mssql_fetch_array($res_sql)){
echo 'new google.maps.LatLng('.$ar_sql['LATITUDE'].','.$ar_sql['LONGITUDE'].'), ';
} ?>
];
var carPath = new google.maps.Polyline({
path: Ruta,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 4
});
carPath.setMap(map);
<?Php $cont++; } ?>
}
En la función unidades es donde hago la consulta a la BD.