Check if graphic exist at Lat and Long

2707
3
Jump to solution
08-07-2012 11:23 AM
LuisGarcia2
Occasional Contributor II
I have a map where I have a bunch of graphics displayed on a graphics layer. I need to add some new graphics but I don't want to add to a lat/long combination that already has one. Is there any method/function that can do this for me? If not, any suggestion on what is the most efficient way to find out this information?
Thanks!
0 Kudos
1 Solution

Accepted Solutions
JenniferNery
Esri Regular Contributor
There's no method that checks if lat/lon already exist, but you can use Linq query to do that:

The following code checks if any graphic with x/y value exist. Note that the precision need to match in order for this function to return true.
   var lon = -140.9;    var lat = 63.391;    var found = l.Graphics.Any(g => g.Geometry is MapPoint && Math.Round((g.Geometry as MapPoint).X, 1) == lon     && Math.Round((g.Geometry as MapPoint).Y, 3) ==lat);

View solution in original post

0 Kudos
3 Replies
deleted-user-ATjHIWsdQYmT
Occasional Contributor III
I have a map where I have a bunch of graphics displayed on a graphics layer. I need to add some new graphics but I don't want to add to a lat/long combination that already has one. Is there any method/function that can do this for me? If not, any suggestion on what is the most efficient way to find out this information?
Thanks!


Assuming these are points, You could loop through all of your graphics in your GraphicsLayer casting it's the geometry to a MapPoint.  take the x/y of the graphic and compare it to the one you want to add.  if it's the same then exit your loop.  Thus, not adding the point


Psudo-Code....

For each gra as Graphic in myGraphicsLayer.graphics()
if (trycast(gra,MapPoint).X = YourNewXCoord) and (trycast(gra,MapPoint).Y = YourNewYCoord) Then
exit for
end if

Next
0 Kudos
JenniferNery
Esri Regular Contributor
There's no method that checks if lat/lon already exist, but you can use Linq query to do that:

The following code checks if any graphic with x/y value exist. Note that the precision need to match in order for this function to return true.
   var lon = -140.9;    var lat = 63.391;    var found = l.Graphics.Any(g => g.Geometry is MapPoint && Math.Round((g.Geometry as MapPoint).X, 1) == lon     && Math.Round((g.Geometry as MapPoint).Y, 3) ==lat);
0 Kudos
LuisGarcia2
Occasional Contributor II
There's no method that checks if lat/lon already exist, but you can use Linq query to do that:

The following code checks if any graphic with x/y value exist. Note that the precision need to match in order for this function to return true.
   var lon = -140.9;
   var lat = 63.391;
   var found = l.Graphics.Any(g => g.Geometry is MapPoint && Math.Round((g.Geometry as MapPoint).X, 1) == lon
    && Math.Round((g.Geometry as MapPoint).Y, 3) ==lat);


Thanks Jennifer! That works great!
0 Kudos