Select to view content in your preferred language

Get latitude and longitude from featurelayer point geometry returned from query

287
2
Jump to solution
04-09-2025 10:03 AM
PeteVitt
Frequent Contributor

Hi I'm working with Javascript Maps SDK version 4.31.  I'm running a query on a featurelayer returning the geometry but I'm not able to access latitude, longitude, x or y properties.  Here's my code:

          let query = featureLayer.createQuery();
          query.returnGeometry = true;
          query.where = "1=1";
          query.outFields = ["OBJECTID", "Permittee"];
          query.orderByFields = ["Permittee"];
          featureLayer.queryFeatures(query)
          .then(function(response){
            for (let i=0; i < response.features.length; i++){
              let data: DischListData = new DischListData();
              data.Permittee = response.features[i].attributes.Permittee;
              data.ObjectID = response.features[i].attributes.OBJECTID;
              //TRYING TWO WAYS TO GET THE DATA
              //1) THESE THROW PROPERTY DOES NOT EXIST ERROR
              data.Lat= response.features[i].geometry.latitude;
              data.Long= response.features[i].geometry.longitude;
             
              //2) THESE ARE NULL
              data.Lat= response.features[i].geometry.extent.center.longitude;
              data.Long= response.features[i].geometry.extent.center.longitude;
              a.listData.push(data);
            }
            console.log(a.listData);

          });
I can see the latitude and longitude property values in my debug console (see attached screen shot) but cant access them in my code.  I've also tried to access the graphic property from my returned  features, but not having any luck there either.  Does anyone know how I can get lat, long, x, and y? Ultimately I want to zoom to a point when a user selects the name of a Permittee from a dropdown.
 
Thanks
 
Pete
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
PeteVitt
Frequent Contributor

Thanks Joel -- the test for null was a good suggestion.  I didn't find any null geometries in my featurelayer, but I was able to get the lat and long values by putting the geometry of each record into a variable, and then pulling the lat and longs from the variable.  I don't know why this works. I'm using Angular so maybe something with the strict types?

             data.Permittee = response.features[i].attributes.Permittee;
              data.ObjectID = response.features[i].attributes.OBJECTID;
              data.Geom = response.features[i].geometry;
              data.Lat = data.Geom.latitude;
              data.Long = data.Geom.longitude;

View solution in original post

2 Replies
JoelBennett
MVP Regular Contributor

As far as I can tell, your code looks fine.  Here's something to look into though:

  1. Your query's where clause "1=1" will return every record in the layer (this is and of itself is fine).
  2. Your console window only shows one record, which itself appears to be fine.
  3. However, it's possible that one record somewhere in the midst of all the records returned by the query has a null geometry.  Such a case could cause the problem you're seeing.  In that case, you might want to add a check to see if a record has a geometry before processing it.
0 Kudos
PeteVitt
Frequent Contributor

Thanks Joel -- the test for null was a good suggestion.  I didn't find any null geometries in my featurelayer, but I was able to get the lat and long values by putting the geometry of each record into a variable, and then pulling the lat and longs from the variable.  I don't know why this works. I'm using Angular so maybe something with the strict types?

             data.Permittee = response.features[i].attributes.Permittee;
              data.ObjectID = response.features[i].attributes.OBJECTID;
              data.Geom = response.features[i].geometry;
              data.Lat = data.Geom.latitude;
              data.Long = data.Geom.longitude;