Search code examples
c#asp.netjsongoogle-mapsgeojson

embed online database from asp.net on google maps


I've built a project on asp.net and it's have it's own database. This DB is updating all the time with sensors that are connected to arduino and accordind to them the tables in the DB are changing. (To be more specific - there is magnetic sensor under the parking and when a car is getting on this parking , the parking ststus in the database changes to false).

I want to embed this sql database (asp.net) on google maps api on my web-site. So that in my web site I will see google maps and all the parkings with their status (markers?), update real-time with the DB, (green if it's true , Red - if its false)

The database consists of two different tables.

My query is written in LINQ. I've read that I need to use with json / geojson file.

It is correct? Can you explain me more about it? And how can I make this json file always update with the DB ?

THANK YOU!!


Solution

  • https://developers.google.com/maps/documentation/javascript/

    Take a look at the documents linked above. I would just use JavaScript perhaps consuming your data via REST API and periodically update the map by rechecking the data.

    You will need sign up for a key with Google. You can get one for free which gives you a finite number of requests over a given time. If you require to use more you'd need to pay.

    If you didn't want to bother with REST and just wanted one update per load of page you could utilise Page_Load on your page and pass your data to client side/JavaScript that way. Something like the below. But I would favour the web services way as you can update without the need to reload the page.

    protected void Page_Load(object sender, EventArgs e)
            {
                var sqlData;
                using (SqlDataDataContext ctx = new SqlDataDataContext(ConfigurationManager.ConnectionStrings["ConnectionDB"].ConnectionString))
            {
                sqlData = ctx.GetCurrentData();
            }
            StringBuilder sbJs = new StringBuilder();
            sbJs.AppendLine(@"var sqlData = new Array();");
            int i = 0;
            foreach(var sqlRow in sqlData)
            {
                sbJs.AppendLine(String.Format("sqlData.push({id:{0}, latitude:{1}, longitude:{2}, status:{3}});", sqlRow.Id, sqlRow.latitude , sqlRow.longitude, sqlRow.status);
            }
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DynamicScript", sbJs.ToString(), true);
        }