﻿// JScript File

        // Javascript pour la carte Virtual Earth
        var myVEMap = null;
        var gmap = null;
        var circlePrecision = 5;    // 1 = max ; 5 = très bien ; 10 = correct

        //Draw Localisation
        //Dessine un point si loc gps, ou un cercle si loc cell id
        //pRadius : rayon en km
        //pIcon : icone pour la localisation
        //renvoit la shape creee
        function DrawLoc(pbooVEMap, pLat, pLong, pRadius, pTitre, pLibelle, pIcon, pLayer)
        {
            var shape = null;

            // Si rayon spécifié, on dessine un cercle (loc cell id)
            if( pRadius > 0 )
            {
                // Si VirtualEarth
                if(pbooVEMap)
                {
                    VEDrawCircle(pLat, pLong, pRadius);
                }
                else
                {
                    // Si GMap
	                GDrawCircle(pLat, pLong, pRadius);
	            }
            }
            else
            {
                // Point à dessiner (loc GPS)
                
                // Si VirtualEarth
                if(pbooVEMap)
                {
                    var loc = new VELatLong(pLat, pLong);
                    shape = AddPushpin(pIcon, loc, pTitre, pLibelle, pLayer);
                }
                else
                {
                    // Si GMap
                    var position = new GLatLng(pLat, pLong);

                    // icon est une variable globale définie dans le .vb...
                    marker = new GMarker(position, icon);
                    gmap.addOverlay(marker);

                    // Si titre à afficher
                    if( pTitre != "" )
                    {
                        gmap.openInfoWindow(position, document.createTextNode( pTitre ));
                    }
                }
            }
            
            return shape;
        }
        
        //renvoit la shape creee
        function AddPushpin(pIcon, Loc, Title, Description, pLayer)
        {   
            var shape = new VEShape(VEShapeType.Pushpin,Loc);
            
            // Si pas de layer specifiee, on prend la map par defaut
            if(pLayer==null) pLayer = myVEMap;
            
            shape.SetTitle(Title); 
            shape.SetDescription(Description);          

            // L'objet VECustomIconSpecification est necessaire pour voir l'image en 3D
            // Et il faut donner le chemin absolu vers l'image pour la voir en 3D : voir http://msdn.microsoft.com/en-us/library/bb545007.aspx
            var spec = new VECustomIconSpecification();
            spec.Image = pIcon
            shape.SetCustomIcon(spec);
            
            pLayer.AddShape(shape);
            
            return shape;
        }

        // Fonction drawCircle pour VE
        function VEDrawCircle(pLatCenter, pLongCenter, pRadius)
        {
            var R = 6371; // earth's mean radius in km
            R = R*1000; // conversion en metres
            var lat = (pLatCenter * Math.PI) / 180; //rad
            var lon = (pLongCenter * Math.PI) / 180; //rad
            var d = parseFloat(pRadius) / R;  // d = angular distance covered on earth's surface
            var lCirclePoints = new Array();
            for (x = 0; x <= 360; x+=circlePrecision) 
            { 
                brng = x * Math.PI / 180; //rad
                latitude = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
                longitude = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(latitude))) * 180) / Math.PI;
                latitude = (latitude * 180) / Math.PI;

                var p2 = new VELatLong(latitude,longitude)            
                lCirclePoints.push(p2);
            }
            
            VECircle = new VEShape(VEShapeType.Polygon, lCirclePoints);
            VECircle.HideIcon();
            myVEMap.AddShape(VECircle);
        }

        // Fonction drawCircle pour GMap
        var normalProj = G_NORMAL_MAP.getProjection();
        function GDrawCircle(pLatCenter, pLongCenter, pRadius)
        {
	        var zoom = gmap.getZoom();
            var centerPoint = new GLatLng(pLatCenter,pLongCenter);
	        var centerPt = normalProj.fromLatLngToPixel(centerPoint, zoom);

	        var circlePoints = Array();

	        with (Math) {
	            // Rayon (en km) (conversion des pieds en km)
		        var radius = pRadius * 3.2808/2 / 10; //floor(sqrt(pow((centerPt.x-radiusPt.x),2) + pow((centerPt.y-radiusPt.y),2)));

		        for (var a = 0 ; a < 361 ; a+=circlePrecision ) {
			        var aRad = a*(PI/180);
			        y = centerPt.y + radius * sin(aRad)
			        x = centerPt.x + radius * cos(aRad)
			        var p = new GPoint(x,y);
			        circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
		        }

		        circleLine = new GPolyline(circlePoints);
		        gmap.addOverlay(circleLine);
	        }
        }


