Learn Google Map Api With C#
Objective: -To create a web application that will use the google map api to produce longtitude, latitude locations. The application will r...
https://www.czetsuyatech.com/2009/11/c-learn-google-map.html
Objective:
-To create a web application that will use the google map api to produce longtitude, latitude locations. The application will read string address values from an excel document. So it's also an excel reader.
What you need:
1.) Visual Studio IDE (I'm using 2008)
2.) Microsoft Excel (will contain the addresses)
3.) GoogleAPIKey (http://code.google.com/apis/maps/signup.html)
Now we're set. What we should do:
1.) create a new dotnet web application, name it GMapWeb
2.) in our application's web.config, under the configSection section add an entry: <add key="GoogleApiKey" value="yourgoogleapikey"/>
-googleapikey, something like: ABQIAAAAYYO_Kw81Wxrb9OWt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxENLRU965DSD7kFtdhVw
3.) next, we will edit the default page, Default.aspx.cs (codebehind)
a.) onpageload event we will read the address data in the excel document (the excel document must reside on the same location as Default.aspx):
Finally we have to edit the Default.aspx page (front end - ui):
-change the googlekeyapi, with the one you registered with
-on button click, the application will call the javascript function showAddress, it will create a blank document to display the address string and it's latitude, longtitude coordinate
-GClientGeocoder class, is responsible for processing string address information and converting it to longtitude, latitude, there are more functions available just consult the api from google
-sample printed data: 1 Star Drive Laguna Niguel :sachiko: (33.541886, -117.676792)
BTW, I think there's no need for me to attach a sample excel document. Just create a blank excel document and enter the following address data in the first column, you can add more if you like
-To create a web application that will use the google map api to produce longtitude, latitude locations. The application will read string address values from an excel document. So it's also an excel reader.
What you need:
1.) Visual Studio IDE (I'm using 2008)
2.) Microsoft Excel (will contain the addresses)
3.) GoogleAPIKey (http://code.google.com/apis/maps/signup.html)
Now we're set. What we should do:
1.) create a new dotnet web application, name it GMapWeb
2.) in our application's web.config, under the configSection section add an entry: <add key="GoogleApiKey" value="yourgoogleapikey"/>
-googleapikey, something like: ABQIAAAAYYO_Kw81Wxrb9OWt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxENLRU965DSD7kFtdhVw
3.) next, we will edit the default page, Default.aspx.cs (codebehind)
a.) onpageload event we will read the address data in the excel document (the excel document must reside on the same location as Default.aspx):
protected void Page_Load(object sender, EventArgs e) { string st = MappedApplicationPath + "address.xls"; //virtual location of the excel document String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + st + ";" + "Extended Properties=Excel 8.0;"; //set the connection string OleDbConnection objConn = null; ; try { //open connection and execute query objConn = new OleDbConnection(sConnectionString); objConn.Open(); OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn); OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); objAdapter1.SelectCommand = objCmdSelect; DataSet objDataset1 = new DataSet(); objAdapter1.Fill(objDataset1); ArrayList arrAdd = new ArrayList(); //read each entry in excel's document first column foreach (DataTable table in objDataset1.Tables) foreach (DataRow row in table.Rows) { string s = ""; foreach (DataColumn col in table.Columns) { s = s + " " + row[col].ToString(); } arrAdd.Add(s); //add each address to an ArrayList } ViewState["edward"] = arrAdd; //add the ArrayList in a ViewState object } catch (Exception ex) { string s = ex.ToString(); } finally { objConn.Close(); } }
//get the virtual location protected string MappedApplicationPath { get { try { string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower(); if (APP_PATH == "/") //a site APP_PATH = "/"; else if (!APP_PATH.EndsWith(@"/")) //a virtual APP_PATH += @"/"; string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH); if (!it.EndsWith(@"\")) it += @"\"; return it; } catch (Exception nre) { return null; } } //end get } //end mapped
Finally we have to edit the Default.aspx page (front end - ui):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getCoordinate.aspx.cs" Inherits="getCoordinate" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Google Maps API Example - Geocoding API</title> <script src="http://maps.google.com/maps?file=api&v=2.x&key=googlekeyapi" type="text/javascript"></script> <script type="text/javascript"> //<![CDATA[ var map = null; var geocoder = null; function load() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById("map")); map.setCenter(new GLatLng(37.4419, -122.1419), 13); //set the center map to "1600 Amphitheatre Pky, Mountain View, CA" geocoder = new GClientGeocoder(); //create a new Geocoder object } } <% ArrayList arrAdd = (ArrayList)ViewState["edward"]; %> function showAddress() { if (geocoder) { var address = null; var d = open('', ''); //open a blank document <% for(int i = 0; i < arrAdd.Count; i++) { %> address = '<% Response.Write(arrAdd[i].ToString()); %>'; geocoder.getLatLng( address, function(point) { //display the string address and its corresponding longtitude, latitude d.document.write('<% Response.Write(arrAdd[i].ToString()); %> :sachiko: ' + point + '<br />'); } // sample printed data: 1 Star Drive Laguna Niguel :sachiko: (33.541886, -117.676792) ); <% } %> } } //]]> </script> </head> <body onload="load()" onunload="GUnload()"> <form action="#" onsubmit="showAddress(); return false"> <p> <input type="text" size="60" name="address" value="1600 Amphitheatre Pky, Mountain View, CA" /> <input type="submit" value="Go!" /> </p> <div id="map" style="width: 500px; height: 300px"></div> </form> </body> </html>Note:
-change the googlekeyapi, with the one you registered with
-on button click, the application will call the javascript function showAddress, it will create a blank document to display the address string and it's latitude, longtitude coordinate
-GClientGeocoder class, is responsible for processing string address information and converting it to longtitude, latitude, there are more functions available just consult the api from google
-sample printed data: 1 Star Drive Laguna Niguel :sachiko: (33.541886, -117.676792)
BTW, I think there's no need for me to attach a sample excel document. Just create a blank excel document and enter the following address data in the first column, you can add more if you like
1 Star Drive Laguna Niguel 1011 Brioso Drive, Suite 108 Costa Mesa 10605 Bechler River Ave Fountain Valley 10605 Bechler River Ave. Fountain Valley 10900 Firestone Blvd Norwalk 11800 Woodruff Ave. Downey 1201 Normandy Pl Santa Ana 1227 S LA BREA AVE INGLEWOOD 1290 Knollwood Circle Anaheim
1 comment
hi,
First of all. Thanks very much for your useful post.
I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.
Please let me introduce you some info related to this post and I hope that it is useful for .Net community.
There is a good C# resource site, Have alook
http://csharptalk.com
simi
Post a Comment