﻿      var map = null;

function log(message)
{
debug.value += message+ "\n";
}
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function  mapclick(e)
{

  try
  {
  var LL = null;
  if (map.GetMapMode()==VEMapMode.Mode2D)
  {
    var x = e.mapX;
    var y = e.mapY;
    pixel = new VEPixel(x, y);
    LL  = map.PixelToLatLong(pixel);
    log(LL);
  }
  
  else{
        LL = e.latLong;
  }
  
  if (LL!=null)
  {
  log("Get LatLong properties");
    var long = LL.Longitude;
    if (long < 0 && map.GetMapMode()==VEMapMode.Mode3D) long = 360+ long;
    var lat = LL.Latitude;

   geom.value = geom.value + (geom.value ==""?"":",")  + lat + " " + long;
   }
  }
  catch (Ex)
  {
    alert (Ex.message);
  }

}
function GoToMicrosoft(){
map.SetMapMode(VEMapMode.Mode2D);
map.SetZoomLevel(14);
geom.value="47.64625090207727 -122.1207618713379,47.640815101659676 -122.12067604064941,47.63879098890475 -122.1233367919922,47.63549440885407 -122.12994575500487,47.635205225242885 -122.13252067565916,47.63341225113221 -122.13269233703612,47.633470089967 -122.13818550109864,47.63711380750112 -122.13818550109864,47.638443990272826 -122.13629722595216,47.640121128977846 -122.1350955963135,47.64127774499297 -122.13466644287111,47.64168255454883 -122.13458061218264,47.64231867751335 -122.13380813598635,47.64266565041103 -122.13363647460937,47.64283913599568 -122.13260650634768,47.646366551259895 -122.13260650634768,47.646366551259895 -122.1207618713379,47.64625090207727 -122.1207618713379";
LoadShape ();
map.SetMapMode(VEMapMode.Mode3D);

}
      function GetMap()
      {
         map = new VEMap('myMap');
         map.LoadMap();
     	 map.SetZoomLevel(1);
	    try{
            map.AttachEvent("onclick", mapclick);
           }
          catch (ex) 
       
        {
         debug.value += ex.message;
          alert(ex.message);
        }
      }
function ClearBox()
{
  geom.value = "";
  log("Clear Box");
}


function ClearMap()
{
    map.DeleteAllShapes();
    log("Delete shapes");
}

function LoadShape()
{
  var arr ;
  arr =geom.value.split(",");

try{
  var Points = new Array(arr.length);
  log("Points in shape " + arr.length);
  for (var point in arr )
  {
     var thispoint = ltrim(arr[point]).split(" ");
     thispoint[0] = Math.round(thispoint[0]*10000)/10000;
     thispoint[1] = Math.round(thispoint[1]*10000)/10000;
     log("Point " + point + " (" + thispoint + ") " + thispoint[0]+ ":" + thispoint[1]);
     
     Points[point] = new VELatLong(thispoint[0],thispoint[1], 0, VEAltitudeMode. RelativeToGround);
  }


  log("Creating shape");
  var myPolygon = new VEShape(VEShapeType.Polygon, Points);
//  myPolygon.SetTitle("My Polygon");
  //myPolygon.SetDescription("This is the description for my polygon.");

  log("Adding shape");
  map.AddShape(myPolygon);
  log("Shape added");
  map.SetCenter(Points[0]);
}
catch (ex)
{
log(ex.message);
}

}



