﻿/** This function is used for address locations. Microsoft virtual Earth map method is 
 used.**/


   function locate() {

       var veMap = new VEMap('veMap');
       var index = 0;
        veMap.LoadMap();
        addressText = dojo.byId("text1").value;
        map.graphics.clear();
        dojo.byId("addressResult").innerHTML = "";
        try {
            veMap.Find(null, addressText, null, null, index, 10, true, true, false, true, GetResults);
            index = parseInt(index) + 9;
        }
        catch (Error) {
            window.alert(Error.message);
        }
    }

    function GetResults(layer, resultsArray, places, hasMore, veErrorMessage) {
        if (places != null) {
            if (places.length > 1) {
                var results = "Did you mean:\n";
                for (var x = 0; x < places.length; x++) {
                    results += "<span style=\" text-decoration: underline;color:'blue' \" onmouseover=\"this.style.color='red';this.style.cursor='hand';\" onmouseout=\"this.style.color='blue';\" onclick='showPoint(" + places[x].LatLong.Longitude + "," + places[x].LatLong.Latitude + ")';>" + places[x].Name + "</span><br/>";
                }
                dojo.byId("addressResult").innerHTML = results;

            }
            else {
                findPlaceResults = places[0].LatLong;
                showPoint(findPlaceResults.Longitude, findPlaceResults.Latitude);
            }
        }
        else {
            dojo.byId("addressResult").innerHTML = "*address not found";
        }

    }

    function showPoint(x, y) {


        try {
             var points = new esri.geometry.Multipoint(map.spatialReference);
        var expand = 5;
        var location = new esri.geometry.Point(parseFloat(x), parseFloat(y), new esri.SpatialReference(map.spatialReference));
        if (expand > 0) {
            var expandLocation1 = new esri.geometry.Point(parseFloat(x) + 0.01, parseFloat(y) + 0.01, new esri.SpatialReference(map.spatialReference));
            var expandLocation2 = new esri.geometry.Point(parseFloat(x) - 0.01, parseFloat(y) - 0.01, new esri.SpatialReference(map.spatialReference));
            points.addPoint(expandLocation1);
            points.addPoint(expandLocation2);
        }
        map.setExtent(points.getExtent().expand(expand));
        var evt = { mapPoint: location };
        doQuery(evt);

        points = null;
        dojo.byId("addressResult").innerHTML = "";
           
        }
        catch (Error) {
            alert(Error.message);
        } 

       
    }