Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
13,899,545
Sign up formembers
our free weekly Web Dev Newsletter. Sign in
×
A simple web page that would allow the user to see all locations where their service could be found
Introduction
The client wanted to display a simple web page that would allow the user to see all locations where their service could be found.
Background
I believe that simple problems should have simple solutions. In this case, I was asked to use ASP. I elected to avoid the Entity-
Framework and Model-View-Controller approach. Instead, I hoped to find a way to do all the work in a Razor Page. To do this, I
would need:
1. Google-Maps key
2. A way to outline the state/area
3. A way to send C# DataTable results to JavaScript
https://www.codeproject.com/Tips/1280548/Simple-Razor-Page-using-SQL-JavaScript-and-Google 1/5
22/03/2019 Simple Razor Page using SQL, JavaScript and Google-Maps - CodeProject
SqlCommand sql = new SqlCommand("select top 10 [name], lat,
lng from Sites where (lng is not null) and (lng is not null); ", conn);
SqlDataReader rows = sql.ExecuteReader();
while (rows.Read())
{
table.Rows.Add(rows.GetValue(0), rows.GetValue(1), rows.GetValue(2));
@* <text>@rows.GetValue(1), </text> *@
@* <text> @table.Rows[0][0] </text> *@
}
string j_string = @JsonConvert.SerializeObject(table);
}
The next section of code links to the Google maps and creates a map object. Note that you will have to use/generate your own key.
Be sure to enable it for the actions you will perform, e.g., placing markers on the map, getting directions, etc.
I wanted to add a border around the state in order to draw emphasis to do it. While the good people at Google did provide a tool
to create a “poly line”, you will need to provide the data points on your own. The USGS provides this data but I could not find a site
that provided what I needed from them. Happily, I was not the first one with this problem. Someone else solved it and graciously
provided the answer at their web site: http://eric.clst.org/tech/usgeojson/.
(Thanks Eric!)
Because of the large number of data points, I created a separate file to hold the “outline” array. It looks like this:
This line of code will start the process of getting the SQL data into a form that JavaScript could use. Note that the variable
“j_string” from the Razor code above is preceded with an at-sign and is in quotation marks.
However, instead of being in pure JSON format, the quotation marks are presented in HTML language, e.g., “"”. So we need to
replace all occurrences with actual quotation marks:
https://www.codeproject.com/Tips/1280548/Simple-Razor-Page-using-SQL-JavaScript-and-Google 2/5
22/03/2019 Simple Razor Page using SQL, JavaScript and Google-Maps - CodeProject
We can now parse the string into a JSON object, select each element of the object and pass it to the Google maps market logic:
History
21st March, 2019: Initial version
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
Simple CRUD Using Backbone.js in ASP.NET MVC Simple HTML5 Spiro Drawing Page
Razor
Search Comments
Refresh 1
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
https://www.codeproject.com/Tips/1280548/Simple-Razor-Page-using-SQL-JavaScript-and-Google 5/5