﻿ var myMap = null;
 
 var confidenceStrings = ["High", "Medium", "Low"];
 var precisionStrings  = ["Interpolated", "Rooftop"];

 var pushpinUrls       = ["virtualearthimages\green.png", "virtualearthimages\orange.png", "virtualearthimages\red.png"];

 function LoadMap()
 {
    myMap = new VEMap("mapDiv");
    myMap.LoadMap();
 }

 function UnloadMap()
 {
    if (myMap != null) {
       myMap.Dispose();
    }
 }
 
 function StartGeocoding(address)
 {
    myMap.Find(null,         // what
          address,           // where
          null,              // VEFindType (always VEFindType.Businesses)
          null,              // VEShapeLayer (base by default)
          null,              // start index for results (0 by default)
          null,              // max number of results (default is 10)
          null,              // show results? (default is true)
          null,              // create pushpin for what results? (ignored since what is null)
          null,              // use default disambiguation? (default is true)
          null,              // set best map view? (default is true)
          GeocodeCallback);  // call back function
 }
 
 function GeocodeCallback(shapeLayer, findResults, places, moreResults, errorMsg)
 {
    var resHtml = "";

    // if there are no results, display the error message and return
    if (places == null)
    {
       alert( (errorMsg == null) ? "There were no results" : errorMsg );
       return;
    }

   // get the most important place
   var p = 0
   // Gather some info up front
   var place = places[p];
   var location = place.LatLong;
   var confString = confidenceStrings[place.MatchConfidence];
   var precString = precisionStrings[place.Precision];
   var mcVal = MatchCode(place.MatchCode);
   var latitude = location.Latitude;
   var longitude = location.Longitude;

   // Create a pin at that location, list the latitude & longitude
   var pin = new VEShape(VEShapeType.Pushpin, location);
   pin.SetCustomIcon("<img src='/virtualearthimages/checkbox_off.gif'>");
   pin.SetTitle(title);
   pin.SetDescription(desc);
   myMap.AddShape(pin);
 }
 
 function MatchCode(code)
 {
    if(code == VEMatchCode.None) {
       return "No match";
    }

    var codeDesc = "";   
    var cVal;
    
    cVal = code & VEMatchCode.Good;
    if(cVal > 0) {
      codeDesc += "Good ";
    }

    cVal = code & VEMatchCode.Ambiguous;
    if(cVal > 0) {
      codeDesc += "Ambiguous ";
    }

    cVal = code & VEMatchCode.UpHierarchy;
    if(cVal > 0) {
      codeDesc += "UpHierarchy ";
    }

    cVal = code & VEMatchCode.Modified;
    if(cVal > 0) {
      codeDesc += "Modified ";
    }
    
    return(codeDesc + "Match");
 }
          
 function onGeocodeClick()
 {
    myMap.Clear();
    address = document.getElementById("txtWhere").value;
    StartGeocoding(address);
 }

 function onShowOnMapClick(address)
 {
    myMap.Clear();    
    StartGeocoding(address);
 }
 
 function loadListingsOnMap()
 {
    myMap.Clear();
    
    if(arrListings.length > 0)
    {        
        for(var i = 0; i < arrListings.length; i++)
        {
            var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(arrListings[i][0],arrListings[i][1])); 
            pin.SetCustomIcon("<img src='/virtualearthimages/checkbox_off.gif'>"); 
            pin.SetTitle(arrListings[i][2]); 
            pin.SetDescription(arrListings[i][3]); 
            myMap.AddShape(pin);  
        }
    
        myMap.SetZoomLevel(10);
        myMap.SetCenter(new VELatLong(arrListings[0][0],arrListings[0][1]));
    }    
 }

