Change point location -V.4.0

1894
7
07-29-2016 09:03 AM
hebasaleh
New Contributor II

i am using esri javascript v.4 ,if i have a services that push lat,long for point how can i change the point location on map without add and remove it ,also if i need to change  its symbol and attributes .

0 Kudos
7 Replies
thejuskambi
Occasional Contributor III

If you are talking about, setGeometry and setSymbol methods that were there in 3.x (Graphic), you dont need them any more. In 4.x you can update the properties directly. I would suggest you to go through this Working with properties | ArcGIS API for JavaScript 4.0

hebasaleh
New Contributor II

i tried to update the geometry as below but it didn't work

  var Layer =  this._service.map.layers.getItemAt(0);

       Layer.graphics.getItemAt(0).geometry.latitude += 0.001;

       Layer.graphics.getItemAt(0).geometry.longitude += 0.001;

assuming that i have only one graphic layer and only one graphic on it ,but nothing change.how can i write the path

on this._service.map.get("") if i have many graphic layers with many graphic

0 Kudos
ReneRubalcava
Frequent Contributor

There are a couple of ways you can update existing graphics on the Map in 4.0.

You can use the FeatureLayer#source or GraphicsLayer#graphics.

If you want to update a Graphic, you can do it by cloning the graphics, update the clone, remove the original and add the update.

Something like this.

var originalGraphic = featureLayer.source.getItemAt(2);
var graphic = originalGraphic.clone();
graphic.geometry = updatedGeometry;
graphic.symbol = updatedSymbol;
featureLayer.source.removeAt(2);
featurelayer.source.add(graphic);

That should work.

0 Kudos
hebasaleh
New Contributor II

thanks for your reply, i am using Graphic layer not feature layer,and the graphic layer doesn't contain Clone,so i am doing the following

this.point.latitude += 0.001;

        this.point.longitude += 0.001;

       var Layer =  this._service.map.layers.getItemAt(0);

       var graphic =new Graphic({

           geometry: this.point,

           attributes: {

               index: this._pointIndex

           },

           symbol: this.sym

       });

       Layer.graphics.getItemAt(0);

       Layer.graphics.removeAt(0);

       Layer.graphics.add(graphic);

which i didn't like as i want to update the geometry without adding and removing the graphic again

0 Kudos
thejuskambi
Occasional Contributor III

Rather than updating the latitude and longitude, I would suggest you could simply clone the geometry and try updating the Geometry of the graphic.

var graphic = Layer.graphics.getItemAt(0);

var newPoint = this.point.clone();

//updated the newPoint

graphic.geometry = newPoint;

0 Kudos
hebasaleh
New Contributor II

i tried the following

var graphicItem = this.graphicLayer1.graphics.find((x) => x.attributes["index"] == device.DeviceCode);

               

                var newPoint = graphicItem.geometry;

                var addpoint = newPoint.clone();

                addpoint.longitude = device.Location.Longitude;

                addpoint.latitude = device.Location.Latitude;

                //updated the newPoint

                graphicItem.geometry = addpoint;

but it didn't work .

0 Kudos
ReneRubalcava
Frequent Contributor

You have to remove and replace the entire graphic from the "graphics" collection for it to work.

0 Kudos