The new customer featurelayer (ploygon) could not display on the map. Thanks!

705
8
05-31-2011 06:55 AM
mikezheng
New Contributor
Please see the code as below:

    <script type="text/javascript">
        dojo.require("dijit.layout.BorderContainer");
        dojo.require("dijit.layout.ContentPane");
        dojo.require("esri.map");
        dojo.require("esri.layers.FeatureLayer");
        dojo.require("esri.dijit.Popup");

        dojo.require("esri.arcgis.utils");
        dojo.require("esri.dijit.Scalebar");
        dojo.require("esri.dijit.editing.Editor-all");
        dojo.require("esri.tasks.geometry");
        var gsvc = null;

        var map;
        var featureLayer;
        var resizeTimer;

        var webmap;

        function init() {

            webmap = "da8e773cb68149a39e06f706c6d7cdb2";
            var itemDeferred = esri.arcgis.utils.getItem(webmap);

            itemDeferred.addCallback(function (itemInfo) {

                var initExtent = new esri.geometry.Extent({ "xmin": -12734442.894989174, "ymin": 4942199.239293063, "xmax": -5342676.5117014535, "ymax": 8459525.5328628, "spatialReference": { "wkid": 102100} });


                //create a popup to replace the map's info window
                var popup = new esri.dijit.Popup(null, dojo.create("div"));
                map = new esri.Map("map", {
                    extent: initExtent,
                    infoWindow: popup
                });

                dojo.place(popup.domNode, map.root);

                var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
                map.addLayer(basemap);

                                dojo.connect(map, "onLayersAddResult", function (results) { requestPhotos(itemInfo); });
                                map.addLayers([featureLayer]);

                //create a feature collection for the flickr photos
                var featureCollectionPolygon = {
                    "layerDefinition": null,
                    "featureSet": {
                        "features": [],
                        "geometryType": "esriGeometryPolygon"
                    }
                };
                featureCollectionPolygon.layerDefinition = {
                    "geometryType": "esriGeometryPolygon",
                    "objectIdField": "ObjectID",
                    "drawingInfo": {
                        "renderer": {
                            "type": "simple",
                            "symbol": {
                                "type": "esriSFS",
                                "style": "esriSFSSolid"
                            }
                        }
                    },
                    "fields": [{
                        "name": "ObjectID",
                        "alias": "ObjectID",
                        "type": "esriFieldTypeOID"
                    }, {
                        "name": "description",
                        "alias": "Description",
                        "type": "esriFieldTypeString"
                    }, {
                        "name": "imagelink",
                        "alias": "imagelink",
                        "type": "esriFieldTypeString"
                    }, {
                        "name": "title",
                        "alias": "Title",
                        "type": "esriFieldTypeString"
                    }]
                };

                //define a popup template
                var popupTemplatePolygon = new esri.dijit.PopupTemplate({
                    title: "{title}",
                    description: "{description}"
                });

                //create a feature layer based on the feature collection
                featureLayerPolygon = new esri.layers.FeatureLayer(featureCollectionPolygon, {
                    id: 'Building Information',
                    infoTemplate: popupTemplatePolygon
                });

                //associate the features with the popup on click
                dojo.connect(featureLayerPolygon, "onClick", function (evt) {
                    map.infoWindow.setFeatures([evt.graphic]);
                    map.infoWindow.show(evt.mapPoint);
                });

                dojo.connect(map, "onLayersAddResult", function (results) { requestPolygon(itemInfo); });

                map.addLayers([featureLayerPolygon]);

                dojo.connect(map, 'onLoad', function (theMap) {
                    dojo.connect(dijit.byId('map'), 'resize', function () { //resize the map if the div is resized
                        clearTimeout(resizeTimer);
                        resizeTimer = setTimeout(function () {
                            map.resize();
                            map.reposition();
                        }, 500);
                    });
                });

            });

        }

        function requestPolygon(itemInfo) {
            var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DOT, new dojo.Color([151, 249, 0, .80]), 3), new dojo.Color([151, 249, 0, 0.45]));

            var polygon = new esri.geometry.Polygon({
                "rings": [
            [
              [-4226661.916056009, 8496372.808143634],
              [-3835304.3312360067, 8731187.359035634],
              [-2269873.991956003, 9005137.668409634],
              [-4304933.433020009, 7635386.121539632],
              [-4304933.433020009, 7674521.880021632],
              [-4226661.916056009, 8496372.808143634]
            ]
          ],
                "spatialReference": {
                    "wkid": 102100
                }
            });

            features.push(new esri.Graphic(polygon, polygonSymbol));

            featureLayerPolygon.applyEdits(features, null, null);
        }

        dojo.ready(init);

    </script>
0 Kudos
8 Replies
derekswingley1
Frequent Contributor
Please elaborate on what you've tried and what, specifically, is failing.
0 Kudos
HemingZhu
Occasional Contributor III
Please elaborate on what you've tried and what, specifically, is failing.


mike, a couple of issues in your function requestPolygon(itemInfo). 1. features is undefined. 2. the attributes of the feature you added to your featureLayerPolygon are missing. Try this:

function requestPolygon(itemInfo) {
            var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DOT, new dojo.Color([151, 249, 0, .80]), 3), new dojo.Color([151, 249, 0, 0.45]));

            var polygon = new esri.geometry.Polygon({
                "rings": [
            [
              [-4226661.916056009, 8496372.808143634],
              [-3835304.3312360067, 8731187.359035634],
              [-2269873.991956003, 9005137.668409634],
              [-4304933.433020009, 7635386.121539632],
              [-4304933.433020009, 7674521.880021632],
              [-4226661.916056009, 8496372.808143634]
            ]
          ],
                "spatialReference": {
                    "wkid": 102100
                }
            });

            var attr = {};
            attr["description"] = your description string;
            attr["title"] = your title string;
            featureLayerPolygon.applyEdits([new esri.Graphic(polygon, polygonSymbol, attr)], null, null);
        }
0 Kudos
mikezheng
New Contributor
Please elaborate on what you've tried and what, specifically, is failing.


I want to generate a featurelayer with polygon and add it to map. Actually, i could add it to map thru map.graphics.add(); But i want to use featurelayer.

Thanks.
0 Kudos
mikezheng
New Contributor
mike, a couple of issues in your function requestPolygon(itemInfo). 1. features is undefined. 2. the attributes of the feature you added to your featureLayerPolygon are missing. Try this:

function requestPolygon(itemInfo) {
            var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DOT, new dojo.Color([151, 249, 0, .80]), 3), new dojo.Color([151, 249, 0, 0.45]));

            var polygon = new esri.geometry.Polygon({
                "rings": [
            [
              [-4226661.916056009, 8496372.808143634],
              [-3835304.3312360067, 8731187.359035634],
              [-2269873.991956003, 9005137.668409634],
              [-4304933.433020009, 7635386.121539632],
              [-4304933.433020009, 7674521.880021632],
              [-4226661.916056009, 8496372.808143634]
            ]
          ],
                "spatialReference": {
                    "wkid": 102100
                }
            });

            var attr = {};
            attr["description"] = your description string;
            attr["title"] = your title string;
            featureLayerPolygon.applyEdits([new esri.Graphic(polygon, polygonSymbol, attr)], null, null);
        }


Hi hzhu,

Thanks a lot for your code.

But it still doesn't work.

Thanks again.
0 Kudos
HemingZhu
Occasional Contributor III
Hi hzhu,

Thanks a lot for your code.

But it still doesn't work.

Thanks again.


Mike, since i don't have your complete code, i modified the ESRI sample (http://help.arcgis.com/en/webapi/javascript/arcgis/demos/fl/fl_featureCollection.html)  a little bit to include your posted code. It worked. Maybe there are something else in your code (webmap?) that you need look into.
0 Kudos
mikezheng
New Contributor
Mike, since i don't have your complete code, i modified the ESRI sample (http://help.arcgis.com/en/webapi/javascript/arcgis/demos/fl/fl_featureCollection.html)  a little bit to include your posted code. It worked. Maybe there are something else in your code (webmap?) that you need look into.


Hi hzhu,

Thank you very very much for your code. it work now. 🙂

Another question, after i added that featurelayer, how could i retrieve it? i don't know which object should be used, "map" or "esri". i mean i want to get that polygon in a event.

thanks again,

Mike
0 Kudos
HemingZhu
Occasional Contributor III
Hi hzhu,

Thank you very very much for your code. it work now. 🙂

Another question, after i added that featurelayer, how could i retrieve it? i don't know which object should be used, "map" or "esri". i mean i want to get that polygon in a event.

thanks again,

Mike


Mike, you already have an event handler in your code:

//associate the features with the popup on click
dojo.connect(featureLayerPolygon, "onClick", function (evt) {
//evt.graphic will be the polygon feature you clicked at.
//evt.graphic.geometry will the polygon itself you clicked at
map.infoWindow.setFeatures([evt.graphic]);
map.infoWindow.show(evt.mapPoint);
});

Besides that, you can look at the featurelayer library and pick up any event that fits your need: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/featurelayer.htm
0 Kudos
mikezheng
New Contributor
Thanks. Actually, i already defined it named "featurelayer". it is my need. 🙂

Thanks a lot.
0 Kudos