﻿

// ############################################################
// ZoomToFeaturesSynchronously
// ############################################################
function ZoomToFeaturesSynchronously(featureName, featureKeys) {
   
    // Create a new XMLHttpRequest object...
    var xmlHttp = GetXMLHttpRequest();
    var sessionId = _SESSION_ID;
    var mapDefinition = _MAP_DEFINITION;

    // Make the request...
    xmlHttp.open("GET", "../MapGuide/AjaxApi.aspx?sessionId=" + sessionId + "&mapDefinition=" + mapDefinition + "&featureName=" + featureName + "&featureKeys=" + featureKeys + "&command=GET_FEATURE_ZOOM_EXTENTS", false);
    xmlHttp.send(null);

    if (xmlHttp.readyState == 4) {
        // Parse the response...
        if (xmlHttp.responseXML) {
            var xmlDoc = xmlHttp.responseXML.documentElement;
            
            ZoomExtents(xmlDoc);
            
        }
    }
}

// ############################################################
// ZoomToFeatures
// ############################################################
function ZoomToFeatures(featureName, featureKeys) {

    // Create a new XMLHttpRequest object...
    var xmlHttp = GetXMLHttpRequest();
    var sessionId = _SESSION_ID;
    var mapDefinition = _MAP_DEFINITION;

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            // Parse the response...
            if (xmlHttp.responseXML) {
                var xmlDoc = xmlHttp.responseXML.documentElement;

                ZoomExtents(xmlDoc);

            }
        }
    }

    // Make the request...
    xmlHttp.open("GET", "../MapGuide/AjaxApi.aspx?sessionId=" + sessionId + "&mapDefinition=" + mapDefinition + "&featureName=" + featureName + "&featureKeys=" + featureKeys + "&command=GET_FEATURE_ZOOM_EXTENTS", true);
    xmlHttp.send(null);
}

// ############################################################
// ZoomExtents
// ############################################################
function ZoomExtents(xmlDoc) {
    var featureSizeFactor = _ZOOM_TO_FEATURE_SIZE_FACTOR;
    var minimumZoomScale = _MIN_ZOOM_SCALE;
    var defaultZoomScale = _DEFAULT_ZOOM_SCALE;

    try {
        //20090622 RAO - Updated for MGE 2010: Explicitly set these values to FLOAT else the map freezes for 2010...                                       
        var centerX = parseFloat(xmlDoc.getElementsByTagName('CenterX')[0].firstChild.data);
        var centerY = parseFloat(xmlDoc.getElementsByTagName('CenterY')[0].firstChild.data);
        var width = parseFloat(xmlDoc.getElementsByTagName('Width')[0].firstChild.data);
        var height = parseFloat(xmlDoc.getElementsByTagName('Height')[0].firstChild.data);
        var scale = GetMapFrame().CalculateScale1((width * featureSizeFactor), (height * featureSizeFactor), GetMapFrame().mapDevW, GetMapFrame().mapDevH);
   
        // 20081027 JD Handle when scale hits zero
        if (scale < minimumZoomScale) { scale = defaultZoomScale; }
        GetMapFrame().GotoView(centerX, centerY, scale, true, true);
    }
    catch (e) {
        alert(e.message);
    }
}
