none
Is not id a property of the Layer Class? RRS feed

  • Question

  • I am not really a programmer and I am struggling in converting a v6.3 app to v8..that may be my main problem.

    In the Documentation it list id as a property of the layer class however when I try and reference it I get nothing however when I  use ....  map.layers[i]._id that works.  Why would I not be able to simply use map.layers[2].id as implied by the documentation?  How would I know when to preface a property with an underscore .....  BTW I only accidentally got this working through a fat finger typo....

    This does not work

    function GetLayerByTitle(layerTitle) {  
        ///<summary>Return the Layer whose title matches</summary>
        ///<param name="layerTitle" type="string"> This is the Layer name</param>
        var numLayer = map.layers.length;
        var i;
        for (i = 0; i < numLayer; i++) {
            if (map.layers[i].id === layerTitle) {
                return map.layers[i];
                }
            }
                return null;
       }

    This works:

    function GetLayerByTitle(layerTitle) {  
        ///<summary>Return the Layer whose title matches</summary>
        ///<param name="layerTitle" type="string"> This is the Layer name</param>
        var numLayer = map.layers.length;
        var i;
        for (i = 0; i < numLayer; i++) {
            if (map.layers[i]._id === layerTitle) {
                return map.layers[i];
                }
            }
                return null;
       }

    Friday, July 7, 2017 5:56 PM

Answers

  • Ted,

    The method should be getId(), with a lower-case 'd', not getID(). JavaScript is case-sensitive, so those two things are not equivalent.

    • Marked as answer by TedKowal Friday, July 7, 2017 7:47 PM
    Friday, July 7, 2017 7:42 PM

All replies

  • I believe .getId() is what you should be using to get the Id.

    So for your code:

    if(map.layers[i].getId() === layerTitle)

    You can find additional documentation here: https://msdn.microsoft.com/en-us/library/mt712656.aspx

    Friday, July 7, 2017 6:41 PM
  • That did not work either..... the function returns a null.   Only when I use the ._id will it return the correct layer.

    Actually JS errors out when I use the .getID() method.  -- 0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'getID' --

    I am concerned if it is my lack of programming knowledge or I am trying to use the property in a incorrect way.  A fat fingered typo fixing it accidentally without know why does not give me any confidence.

    Ted

    Friday, July 7, 2017 7:31 PM
  • Ted,

    The method should be getId(), with a lower-case 'd', not getID(). JavaScript is case-sensitive, so those two things are not equivalent.

    • Marked as answer by TedKowal Friday, July 7, 2017 7:47 PM
    Friday, July 7, 2017 7:42 PM
  • OMG --- hours looking at it and it still ends up being a typo!!!!!!!

    Yes making the change worked!  I feel more comfortable using a documented method than some mysterious ._id.  Still begs to wonder why does the ._id work?

    Friday, July 7, 2017 7:47 PM
  • The _id is the private property in which that data is stored. The getId function simply does "return _id;". The reason for this is to ensure that you don't change the id of the layer as it would mess up the internal handing of the layer as the unique id is used internally.

    [Blog] [twitter] [LinkedIn]

    Saturday, July 8, 2017 1:09 AM
  • Thanks for the answer!

    Am I to assume that the API reference will not always tell me which of the properties are private?

    Monday, July 10, 2017 3:32 PM
  • Any property name in JavaScript that starts with an underscore is generally considered a private property. In reality nothing is private in JavaScript, thus why the naming convention.

    [Blog] [twitter] [LinkedIn]

    Monday, July 10, 2017 11:55 PM