
function zoomtoArea(areaname) {
    var centerpoint = null;
    var zoomLevel = 19;
    switch (areaname) {
        case "1區":
            centerpoint = [121.43446715, 25.20061081];
            break;
        case "2區":
            centerpoint = [121.43284814, 25.19856968];
            break;
        case "3區":
            centerpoint = [121.43573296, 25.19822402];
            break;
        case "4區":
            centerpoint = [121.43860283, 25.19787819];
            break;
        case "5區":
            centerpoint = [121.43270812, 25.19649481];
            break;
        case "6區":
            centerpoint = [121.42971077, 25.1947072];
            break;
        case "7區":
            centerpoint = [121.43268098, 25.19433188];
            break;
        case "8區":
            centerpoint = [121.43574481, 25.19355211];
            break;
        case "9區":
            centerpoint = [121.43876875, 25.19304827];
            break;
        case "全區":
            centerpoint = [121.434241, 25.197010];
            zoomLevel = 16.7;
            break;
        default:
        // code block
    }

    if (centerpoint) {
        window.view.goTo({
            center: centerpoint,
            zoom: zoomLevel,
            tilt: 0,
            heading: 0
        });
    }
}


/*從資料庫產生feature layer*/

function CreateGraphics(rows) {
    var graphics = [];
    require([
        "esri/Graphic"
    ], function (Graphic) {


        rows.forEach(row => {

            var point = {
                type: "point",
                x: row.x,
                y: row.y,
                z: 0
            };

            var pointGraphic = new Graphic({
                geometry: point,
                attributes: row
            });
            graphics.push(pointGraphic);
        })




    });

    return graphics;
}

function CreateLagend(layers, uiLocation ="bottom-left",uiWidth=150) {
    require([
        "esri/widgets/Legend"
    ], function (Legend) {

        if (window.DLegend) {
            view.ui.remove(window.DLegend);
        }

        var  olayerInfos = [];

        layers.forEach(lyr => {
            var olyr = {layer:lyr};
            olayerInfos.push(olyr);
        });

        window.DLegend = new Legend({
            view: view,
            layerInfos: olayerInfos
        });

        view.ui.add(
            window.DLegend,
            uiLocation
        );
        

    });
    $(".esri-ui-bottom-left").width(uiWidth);
}

function ZoomToEquipLocate(layertitle, pEquipID, objectidColumn, uniqColumn, offsetZ = 24, vtilt = 90, vexpend = 1) {

    require([
        'dojo/_base/lang',
        "esri/layers/FeatureLayer",
        "esri/tasks/support/Query",
        "esri/geometry/Extent"
    ], function (lang, FeatureLayer, Query, Extent) {
        //console.log(window.view);

        var foundLayer = window.view.map.allLayers.find(function (layer) {
            return layer.title === layertitle;
        });
        var queryF = foundLayer.createQuery();
        queryF.outFields = [objectidColumn, uniqColumn];
        queryF.returnGeometry = true;
        queryF.returnz = true;
        queryF.where = uniqColumn + " = '" + pEquipID + "' or " + uniqColumn + " = '" + pEquipID.replace("_", "-") + "'";


        foundLayer.queryExtent(queryF).then(function (response) {
            if (response.count <= 0)
                return;

            //設定平均高程
            var ext = new Extent({
                xmin: response.extent.center.longitude - 0.0001,
                ymin: response.extent.center.latitude - 0.0001,
                xmax: response.extent.center.longitude + 0.0001,
                ymax: response.extent.center.latitude + 0.0001,
                zmax: offsetZ + 1,
                zmin: offsetZ
            });

            view.goTo({
                target: ext.expand(vexpend),
                //zoom: 20,
                speedFactor: 0.5,
                tilt: vtilt
                //heading: 0
            }).catch(function (error) {
                if (error.name != "AbortError") {
                    console.error(error);
                }
            });
        });

        foundLayer.queryFeatures(queryF).then(function (result) {
            if (result.features.length <= 0) {
                alert("無設備位置資訊，無法定位。");
                return;
            }


            var objectIdArray = [];
            result.features.forEach(function (feature) {
                var objectId = feature.attributes[objectidColumn];
                objectIdArray.push(objectId);
            });

            //
            //view.goTo(result.features[0]);

            view.whenLayerView(foundLayer).then(function (officeLayerView) {

                if (!officeLayerView.parent.visible | !officeLayerView.visible) {
                    alert("請先切換至設備所在區域範圍。");
                    return;
                }

                if (window.highlight) {
                    window.highlight.remove();
                }
                // Highlight the feature passing the objectId to the method
                window.highlight = officeLayerView.highlight(objectIdArray);


            });
        });




    });

}



/*從資料庫產生feature layer*/
