Bengi,
Here is a working sample of how to get a FeatureCollections extent:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="https://js.arcgis.com/3.18/esri/css/esri.css">
<style type="text/css">
html,
body,
#map {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<title> by Behseini</title>
<script src="https://js.arcgis.com/3.18/"></script>
<script type='text/javascript'>
var map, featureLayer;
require([
"esri/map",
"esri/layers/FeatureLayer",
"esri/geometry/Point",
"esri/geometry/webMercatorUtils",
"esri/graphic",
"esri/graphicsUtils",
"dojo/on",
"dojo/_base/array",
"dojo/domReady!"
],
function(
Map,
FeatureLayer,
Point,
webMercatorUtils,
Graphic,
graphicsUtils,
on,
array
) {
map = new Map("map", {
basemap: "gray",
center: [-123.141608, 49.245291],
zoom: 10
});
var data = [{
"LATITUDE": 49.2898,
"LONGITUDE": -123.1364,
"SCHOOL_NAME": "King George Sec.",
"ADDRESS": "1755 Barclay St",
"URLLINK": "http://www.vsb.bc.ca/schools/king-george"
}, {
"SCHOOL_NAME": "Britannia Sec.",
"LATITUDE": 49.2752,
"LONGITUDE": -123.0719,
"ADDRESS": "1001 Cotton Drive",
"URLLINK": "http://www.vsb.bc.ca/schools/britannia-secondary"
}, {
"SCHOOL_NAME": "Magee Sec.",
"LATITUDE": 49.2286,
"LONGITUDE": -123.1515,
"ADDRESS": "6360 Maple St",
"URLLINK": "http://www.vsb.bc.ca/schools/magee"
}];
var featureCollection = {
"layerDefinition": null,
"featureSet": {
"features": [],
"geometryType": "esriGeometryPoint"
}
};
featureCollection.layerDefinition = {
"geometryType": "esriGeometryPoint",
"objectIdField": "ObjectID",
"drawingInfo": {
"renderer": {
"type": "simple",
"symbol": {
"type": "esriSMS",
"style": "esriSMSSquare",
"color": [76, 115, 0, 255],
"size": 8,
"angle": 0,
"xoffset": 0,
"yoffset": 0,
"outline": {
"color": [152, 230, 0, 255],
"width": 1
}
}
}
},
"fields": [{
"name": "ObjectID",
"alias": "ObjectID",
"type": "esriFieldTypeOID"
}, {
"name": "latitude",
"alias": "Latitude",
"type": "esriFieldTypeGeometry"
}, {
"name": "longitude",
"alias": "Longitude",
"type": "esriFieldTypeGeometry"
}, {
"name": "name",
"alias": "Name",
"type": "esriFieldTypeString"
}, {
"name": "address",
"alias": "Address",
"type": "esriFieldTypeString"
}, {
"name": "link",
"alias": "School Link",
"type": "esriFieldTypeString"
}]
};
featureLayer = new FeatureLayer(featureCollection, {
id: 'schoolsLayer'
});
map.on("layers-add-result", function(results) {
addData();
});
map.addLayers([featureLayer]);
function addData() {
var features = [];
array.forEach(data, function(school, index) {
var attr = {};
attr["latitude"] = school.LATITUDE;
attr["longitude"] = school.LONGITUDE;
attr["name"] = school.SCHOOL_NAME;
attr["address"] = school.ADDRESS;
attr["link"] = school.URLLINK;
var geometry = new Point(school.LONGITUDE, school.LATITUDE);
var graphic = new Graphic(webMercatorUtils.geographicToWebMercator(geometry));
graphic.setAttributes(attr);
features.push(graphic);
});
featureLayer.applyEdits(features, null, null).then(function(evt) {
map.setExtent(graphicsUtils.graphicsExtent(featureLayer.graphics), true);
});
}
});
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>