
// Intervalo de actualizacion (ms)
tiempoActu=30000;

// Usuario de twitter a seguir
user="PruebasPasolini";

// Url api
urlApi="http://api.twitter.com/1/users/show.json?callback=?&screen_name=";

inicio = true;

// Comprueba posicion y llama a actualizar
function comprobarPosicion() {

	$.getJSON(urlApi + user,function(json){

		// Obtenemos la ubicacion
		var texto=json.status.text;
		
		// Separamos el mensaje de los numeros
		var fragmentos=texto.split('=');
		// Si no hay '=' en el mensaje sera siempre 2, en caso de que existan mas, elegimos el ultimo		
		var numTrozos=fragmentos.length;
		var numeros=fragmentos[numTrozos-1];
		
		// Separamos el primer numero
		var cadenaNum=numeros.split(',');
		var numero1=cadenaNum[0];		
		var numero2=cadenaNum[1];	
		
		// Llamamos a Actualizar
		Actualizar(numero1, numero2);
		
		// Comprobamos la ubicacion pasado un tiempo
		setTimeout("comprobarPosicion()", tiempoActu);
		
	});
}

function pintamapa() {
    if (GBrowserIsCompatible()) {

	// 1. Inicialización
	map = new GMap2(document.getElementById("mapa"));
	map.setCenter(new GLatLng(38.264308, -0.697232), 16);
	map.setMapType(G_HYBRID_MAP);
	map.enableDragging();
	map.enableDoubleClickZoom();
	map.enableScrollWheelZoom();
	map.enableContinuousZoom();

	// 2. Buscamos centro del mapa inicial
	//map.setCenter(new GLatLng(38.264308, -0.697232), 18);

	// 3. Icono furgoneta
	icon = new GIcon();
	icon.image = "imagenes/furgo.png";
	icon.iconAnchor = new GPoint(25, 25);
	//icon.infoWindowAnchor = new GPoint(25, 0);
	icon.iconSize = new GSize(50, 50);

	// 4. Punto en el mapa (furgoneta inicial, no llega a verse)
	furgopoint = new GMarker(new GLatLng(38.264308, -0.697232), icon);
	//map.addOverlay(furgopoint);

	// 5. Trazado Camino
	var polyline = new GPolyline([
		new GLatLng(38.260504, -0.698021),
		new GLatLng(38.260942, -0.698077),
		new GLatLng(38.260988, -0.698825),
		new GLatLng(38.261563, -0.698842),
		new GLatLng(38.262616, -0.698984),
		new GLatLng(38.262363, -0.696444),
		new GLatLng(38.262443, -0.695985),
		new GLatLng(38.262264, -0.694689),
		new GLatLng(38.262412, -0.694534),
		new GLatLng(38.264379, -0.694942),
		new GLatLng(38.264436, -0.694397),
		new GLatLng(38.264486, -0.694282),
		new GLatLng(38.265948, -0.693764),
		new GLatLng(38.267852, -0.693761),
		new GLatLng(38.267833, -0.695084),
		new GLatLng(38.267959, -0.696543),
		new GLatLng(38.268367, -0.696599),
		new GLatLng(38.267694, -0.698369),
		new GLatLng(38.267656, -0.698707),
		new GLatLng(38.267725, -0.700419),
		new GLatLng(38.265160, -0.700563),
		new GLatLng(38.265122, -0.699386),
		new GLatLng(38.265586, -0.699367),
		new GLatLng(38.265874, -0.699204),
		new GLatLng(38.265939, -0.698715),
		new GLatLng(38.266506, -0.698710),
		new GLatLng(38.266946, -0.698699),
		new GLatLng(38.267024, -0.697994),
		new GLatLng(38.267373, -0.697967)
	], "#3333cc", 7);
	map.addOverlay(polyline);

	// 6. Zonas de proyección (rojo)
	var polygon1 = new GPolygon([
            new GLatLng(38.260650, -0.697990),
            new GLatLng(38.260650, -0.697742),
            new GLatLng(38.260490, -0.697724),
            new GLatLng(38.260493, -0.697976),
            new GLatLng(38.260650, -0.697990)
	], "#f33f00", 3, 1, "#ff0000", 0.5);
	map.addOverlay(polygon1);

	var polygon2 = new GPolygon([
            new GLatLng(38.261556, -0.698894),
            new GLatLng(38.261449, -0.698884),
            new GLatLng(38.261449, -0.699055),
            new GLatLng(38.261553, -0.699047),
            new GLatLng(38.261556, -0.698894)
	], "#f33f00", 3, 1, "#ff0000", 0.5);
	map.addOverlay(polygon2);

	var polygon3 = new GPolygon([
            new GLatLng(38.262480, -0.696796),
            new GLatLng(38.262647, -0.696791),
            new GLatLng(38.262650, -0.696965),
            new GLatLng(38.262497, -0.696957),
            new GLatLng(38.262480, -0.696796)
	], "#f33f00", 3, 1, "#ff0000", 0.5);
	map.addOverlay(polygon3);

	var polygon4 = new GPolygon([
            new GLatLng(38.264190, -0.695662),
            new GLatLng(38.263990, -0.695699),
            new GLatLng(38.263979, -0.695455),
            new GLatLng(38.264181, -0.695391),
            new GLatLng(38.264190, -0.695662)
	], "#f33f00", 3, 1, "#ff0000", 0.5);
	map.addOverlay(polygon4);

	var polygon5 = new GPolygon([
            new GLatLng(38.267470, -0.697374),
            new GLatLng(38.267536, -0.697700),
            new GLatLng(38.267338, -0.697684),
            new GLatLng(38.267344, -0.697373),
            new GLatLng(38.267470, -0.697374)
	], "#f33f00", 3, 1, "#ff0000", 0.5);
	map.addOverlay(polygon5);

	//7. Llamamos a la función de Neo =)
	comprobarPosicion();

    }
}

function Actualizar(xfurgo,yfurgo) {
	if (inicio == true ) {
		map.setCenter(new GLatLng(xfurgo,yfurgo), 18);
		inicio=false;
	}
	map.removeOverlay(furgopoint);
	furgopoint = new GMarker(new GLatLng(xfurgo,yfurgo), icon);
	map.addOverlay(furgopoint);
}

pintamapa();

