Displaying spatial coordinates from SQL database

840
4
01-12-2018 09:22 AM
MathewMullan1
New Contributor

I have a SQL database with spatial coordinates that I want to display in a Map View hosted in a web application. I have the Map View already rendered but I need some direction on where to start with getting the data displayed on the map.

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Mathew,

   You will need a web service to query and return the data in Json format. If you have some .Net development skills then a simple Google for Creating a RESTfull web service in .Net will do.

0 Kudos
MathewMullan1
New Contributor

Hi Robert, 

Thank you for the feedback. I am able to return the data with ease but I'm unfamiliar with how to get the actual points on the map using the javascript.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

OK, That is the easy part.

you create point using the Point class and the coordinate you returned from SQL database.

https://developers.arcgis.com/javascript/3/jsapi/point-amd.html#constructors 

And a simple marker symbol

https://developers.arcgis.com/javascript/3/jsapi/simplemarkersymbol-amd.html#constructors 

Then create a graphic from the above two

https://developers.arcgis.com/javascript/3/jsapi/graphic-amd.html#constructors 

and add that to the maps graphics layer

https://developers.arcgis.com/javascript/3/jsapi/map-amd.html#graphics 

Refer to this sample for more specific code:

https://developers.arcgis.com/javascript/3/jssamples/graphics_create_circles.html 

0 Kudos
ThomasColson
MVP Frequent Contributor

The easiest way to do this without a lot of custom code on your front end (which will have to change everytime there's a backend change)...would be to add a trigger to pull the coordinates out of the geography (or geometry) object. This gives you a permanently populated and updated XY field. 

CREATE TRIGGER [dbo].[tigger] 
ON [dbo].[feature class] 
after INSERT,UPDATE NOT FOR REPLICATION
AS 
BEGIN 
 SET NOCOUNT ON; 
 
 UPDATE p SET 
 SHAPE = CASE WHEN i.SHAPE IS NOT NULL 
 THEN p.SHAPE ELSE Geography::STPointFromText('POINT(' 
 + CAST(p.LON AS VARCHAR(20)) + ' ' 
 + CAST(p.LAT AS VARCHAR(20)) + ')', 4269) END, 
 LON = CASE WHEN p.SHAPE IS NULL THEN p.LON ELSE p.SHAPE.Long END, 
 LAT = CASE WHEN p.SHAPE IS NULL THEN p.LAT ELSE p.SHAPE.Lat END

 FROM feature class
 AS p
INNER JOIN 
 inserted AS i 
 ON i.globalid = p.globalid

;
END
0 Kudos