Created
August 19, 2015 11:39
-
-
Save freakynit/b6be724811e96b634d56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data; | |
using System.Configuration; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Security; | |
using System.Web.UI; | |
using System.Web.UI.HtmlControls; | |
using System.Web.UI.WebControls; | |
using System.Web.UI.WebControls.WebParts; | |
using System.Xml.Linq; | |
using System.IO; | |
using System.Xml; | |
using System.Net; | |
public static class Geocoder | |
{ | |
//private static string _GoogleMapsKey = ConfigurationManager.AppSettings["GoogleMapsKey"].ToString(); | |
private static string _GoogleMapsKey = "AIzaSyDqEqd2_fhYORp-8bFXlu3es97qdzeNvco"; | |
public static DataTable GetAddress(string query) | |
{ | |
DataSet dt = new DataSet(); | |
if (string.IsNullOrEmpty(_GoogleMapsKey)) | |
_GoogleMapsKey = ConfigurationManager.AppSettings["GoogleMapsKey"]; | |
#region To obtain Lat and Lon of provided Address | |
/*-----------------To obtain Lat and Lon of provided Address--------------------*/ | |
/// Google.com Geocoder V3 | |
/// Url request to | |
/// http://maps.googleapis.com/maps/api/geocode/xml?address= your address&sensor=false&key=API key obtained from Google | |
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false"; | |
url = String.Format(url, HttpUtility.UrlEncode(query)); | |
XmlNode coords = null; | |
try | |
{ | |
//string xmlString = GetUrl(url); | |
//XmlDocument xd = new XmlDocument(); | |
//xd.LoadXml(xmlString); | |
//XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable); | |
//coords = xd.GetElementsByTagName("location")[0]; | |
} | |
catch { } | |
#endregion | |
Geolocation? gl = null; | |
if (coords != null) | |
{ | |
string sLat = coords.FirstChild.InnerText; //obtains Lat of Address provided | |
string sLong = coords.LastChild.InnerText; //obtains Lon of Address Provided | |
string[] coordinateArray = coords.InnerText.Split(','); | |
if (coordinateArray.Length >= 2) | |
{ | |
gl = new Geolocation(Convert.ToDecimal(sLat), Convert.ToDecimal(sLong)); | |
} | |
/*-------------Two Type of Search can be performed | |
* NearbySearch - Search the type of place as mentioned by the type(optional https://developers.google.com/places/documentation/supported_types) parameter | |
* around the provided Lat (mandatory) and Lon (mandatory) value within the radius mentioned | |
* | |
* | |
* TextSearch - Search the type of place as mentioned by query on the address mentioned on query | |
* */ | |
/*-------------------NearBy Search---------------------*/ | |
#region NearbySearch | |
/* Url Format for request | |
* https://maps.googleapis.com/maps/api/place/nearbysearch/xml?key={0}&location={1},{2}&radius=1000&sensor=false&types=restaurant | |
* | |
* parameter - {0} - API key obtained from Google | |
* {1} - Lan of address provided obtained above | |
* {2} - Lon of address provided obtained above | |
* | |
* key,location,radius,sensor - mandatory fields | |
* */ | |
string sFindRestaurentByNearbySearch = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?key={0}&location={1},{2}&radius=1000&sensor=false"; | |
sFindRestaurentByNearbySearch = String.Format(sFindRestaurentByNearbySearch, _GoogleMapsKey, HttpUtility.UrlEncode(sLat), HttpUtility.UrlEncode(sLong)); | |
try | |
{ | |
string xmlRestNearBy = GetUrl(sFindRestaurentByNearbySearch); | |
//create dataset to get list of restaurant | |
dt = convertStringToDataTable(xmlRestNearBy); | |
//XmlDocument xrestaurentNearBy = new XmlDocument(); | |
//xrestaurentNearBy.LoadXml(xmlRestNearBy); | |
//XmlNamespaceManager xrestaurentmanagerNearBy = new XmlNamespaceManager(xrestaurentNearBy.NameTable); | |
} | |
catch { } | |
if (dt != null && dt.Tables[0].Rows[0][0].ToString() != "ZERO_RESULTS") | |
{ | |
return dt.Tables[1]; | |
} | |
else | |
{ | |
return dt.Tables[0]; | |
} | |
#endregion | |
} | |
else | |
{ | |
/*-------------------Text Search---------------------*/ | |
#region TextSearch | |
/* Url Format for request | |
* https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key= API key obtained from Google | |
* | |
* parameter - {0} - text search query with (type of place to search + in + address) e.g. "restaurants+in+Vashi+Navi Mumbai+India" | |
* */ | |
string sFindRestaurentByTextSearch = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key=" + _GoogleMapsKey; | |
//string sSearch = "restaurants+in+" + query.Replace(",", "+"); | |
string sSearch = query.Replace(",", "+"); | |
sFindRestaurentByTextSearch = String.Format(sFindRestaurentByTextSearch, sSearch); | |
XmlNode xmlRestaurent = null; | |
try | |
{ | |
string xmlRestTextSearch = GetUrl(sFindRestaurentByTextSearch); | |
dt = convertStringToDataTable(xmlRestTextSearch); | |
//XmlDocument xrestaurentTextSearch = new XmlDocument(); | |
//xrestaurentTextSearch.LoadXml(xmlRestTextSearch); | |
//XmlNamespaceManager xrestaurentmanagerTextSearch = new XmlNamespaceManager(xrestaurentTextSearch.NameTable); | |
} | |
catch { } | |
#endregion | |
} | |
if (dt != null && dt.Tables[0].Rows[0][0].ToString() != "ZERO_RESULTS") | |
{ | |
return dt.Tables[1]; | |
} | |
else | |
{ | |
return dt.Tables[0]; | |
} | |
} | |
public static DataSet GetAddress_new(string query, string next_page_tocken) | |
{ | |
DataSet dt = new DataSet(); | |
if (string.IsNullOrEmpty(_GoogleMapsKey)) | |
_GoogleMapsKey = ConfigurationManager.AppSettings["GoogleMapsKey"]; | |
#region To obtain Lat and Lon of provided Address | |
/*-----------------To obtain Lat and Lon of provided Address--------------------*/ | |
/// Google.com Geocoder V3 | |
/// Url request to | |
/// http://maps.googleapis.com/maps/api/geocode/xml?address= your address&sensor=false&key=API key obtained from Google | |
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false"; | |
url = String.Format(url, HttpUtility.UrlEncode(query)); | |
XmlNode coords = null; | |
try | |
{ | |
//string xmlString = GetUrl(url); | |
//XmlDocument xd = new XmlDocument(); | |
//xd.LoadXml(xmlString); | |
//XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable); | |
//coords = xd.GetElementsByTagName("location")[0]; | |
} | |
catch { } | |
#endregion | |
Geolocation? gl = null; | |
if (coords != null) | |
{ | |
string sLat = coords.FirstChild.InnerText; //obtains Lat of Address provided | |
string sLong = coords.LastChild.InnerText; //obtains Lon of Address Provided | |
string[] coordinateArray = coords.InnerText.Split(','); | |
if (coordinateArray.Length >= 2) | |
{ | |
gl = new Geolocation(Convert.ToDecimal(sLat), Convert.ToDecimal(sLong)); | |
} | |
/*-------------Two Type of Search can be performed | |
* NearbySearch - Search the type of place as mentioned by the type(optional https://developers.google.com/places/documentation/supported_types) parameter | |
* around the provided Lat (mandatory) and Lon (mandatory) value within the radius mentioned | |
* | |
* | |
* TextSearch - Search the type of place as mentioned by query on the address mentioned on query | |
* */ | |
/*-------------------NearBy Search---------------------*/ | |
#region NearbySearch | |
/* Url Format for request | |
* https://maps.googleapis.com/maps/api/place/nearbysearch/xml?key={0}&location={1},{2}&radius=1000&sensor=false&types=restaurant | |
* | |
* parameter - {0} - API key obtained from Google | |
* {1} - Lan of address provided obtained above | |
* {2} - Lon of address provided obtained above | |
* | |
* key,location,radius,sensor - mandatory fields | |
* */ | |
string sFindRestaurentByNearbySearch = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?key={0}&location={1},{2}&radius=1000&sensor=false"; | |
sFindRestaurentByNearbySearch = String.Format(sFindRestaurentByNearbySearch, _GoogleMapsKey, HttpUtility.UrlEncode(sLat), HttpUtility.UrlEncode(sLong)); | |
try | |
{ | |
string xmlRestNearBy = GetUrl(sFindRestaurentByNearbySearch); | |
//create dataset to get list of restaurant | |
dt = convertStringToDataTable(xmlRestNearBy); | |
//XmlDocument xrestaurentNearBy = new XmlDocument(); | |
//xrestaurentNearBy.LoadXml(xmlRestNearBy); | |
//XmlNamespaceManager xrestaurentmanagerNearBy = new XmlNamespaceManager(xrestaurentNearBy.NameTable); | |
} | |
catch { } | |
if (dt != null && dt.Tables[0].Rows[0][0].ToString() != "ZERO_RESULTS") | |
{ | |
return dt; | |
} | |
else | |
{ | |
return dt; | |
} | |
#endregion | |
} | |
else | |
{ | |
/*-------------------Text Search---------------------*/ | |
#region TextSearch | |
/* Url Format for request | |
* https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key= API key obtained from Google | |
* | |
* parameter - {0} - text search query with (type of place to search + in + address) e.g. "restaurants+in+Vashi+Navi Mumbai+India" | |
* */ | |
string sFindRestaurentByTextSearch; | |
if (next_page_tocken != "") | |
{ | |
sFindRestaurentByTextSearch = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key=" + _GoogleMapsKey + "&pagetoken=" + next_page_tocken; | |
query = query.Replace(" ", "+"); | |
//sFindRestaurentByTextSearch = "https://maps.googleapis.com/maps/api/place/textsearch/xml?sensor=false&key=" + _GoogleMapsKey + "&pagetoken=" + next_page_tocken; | |
} | |
else | |
{ | |
sFindRestaurentByTextSearch = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key=" + _GoogleMapsKey; | |
} | |
//string sSearch = "restaurants+in+" + query.Replace(",", "+"); | |
string sSearch = query.Replace(",", "+"); | |
sFindRestaurentByTextSearch = String.Format(sFindRestaurentByTextSearch, sSearch); | |
//logClass obj = new logClass(); | |
//obj.MailSenderLogMessage1(sFindRestaurentByTextSearch); | |
XmlNode xmlRestaurent = null; | |
try | |
{ | |
string xmlRestTextSearch = GetUrl(sFindRestaurentByTextSearch); | |
dt = convertStringToDataTable(xmlRestTextSearch); | |
//XmlDocument xrestaurentTextSearch = new XmlDocument(); | |
//xrestaurentTextSearch.LoadXml(xmlRestTextSearch); | |
//XmlNamespaceManager xrestaurentmanagerTextSearch = new XmlNamespaceManager(xrestaurentTextSearch.NameTable); | |
} | |
catch { } | |
#endregion | |
} | |
if (dt != null && dt.Tables[0].Rows[0][0].ToString() != "ZERO_RESULTS") | |
{ | |
return dt; | |
} | |
else | |
{ | |
return dt; | |
} | |
} | |
public static Geolocation? ResolveAddress(string query) | |
{ | |
if (string.IsNullOrEmpty(_GoogleMapsKey)) | |
_GoogleMapsKey = ConfigurationManager.AppSettings["GoogleMapsKey"]; | |
#region To obtain Lat and Lon of provided Address | |
/*-----------------To obtain Lat and Lon of provided Address--------------------*/ | |
/// Google.com Geocoder V3 | |
/// Url request to | |
/// http://maps.googleapis.com/maps/api/geocode/xml?address= your address&sensor=false&key=API key obtained from Google | |
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false"; | |
url = String.Format(url, HttpUtility.UrlEncode(query)); | |
XmlNode coords = null; | |
try | |
{ | |
string xmlString = GetUrl(url); | |
XmlDocument xd = new XmlDocument(); | |
xd.LoadXml(xmlString); | |
XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable); | |
coords = xd.GetElementsByTagName("location")[0]; | |
} | |
catch { } | |
#endregion | |
Geolocation? gl = null; | |
if (coords != null) | |
{ | |
string sLat = coords.FirstChild.InnerText; //obtains Lat of Address provided | |
string sLong = coords.LastChild.InnerText; //obtains Lon of Address Provided | |
string[] coordinateArray = coords.InnerText.Split(','); | |
if (coordinateArray.Length >= 2) | |
{ | |
gl = new Geolocation(Convert.ToDecimal(sLat), Convert.ToDecimal(sLong)); | |
} | |
/*-------------Two Type of Search can be performed | |
* NearbySearch - Search the type of place as mentioned by the type(optional https://developers.google.com/places/documentation/supported_types) parameter | |
* around the provided Lat (mandatory) and Lon (mandatory) value within the radius mentioned | |
* | |
* | |
* TextSearch - Search the type of place as mentioned by query on the address mentioned on query | |
* */ | |
/*-------------------NearBy Search---------------------*/ | |
#region NearbySearch | |
/* Url Format for request | |
* https://maps.googleapis.com/maps/api/place/nearbysearch/xml?key={0}&location={1},{2}&radius=1000&sensor=false&types=restaurant | |
* | |
* parameter - {0} - API key obtained from Google | |
* {1} - Lan of address provided obtained above | |
* {2} - Lon of address provided obtained above | |
* | |
* key,location,radius,sensor - mandatory fields | |
* */ | |
string sFindRestaurentByNearbySearch = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?key={0}&location={1},{2}&radius=1000&sensor=false&types=restaurant"; | |
sFindRestaurentByNearbySearch = String.Format(sFindRestaurentByNearbySearch, _GoogleMapsKey, HttpUtility.UrlEncode(sLat), HttpUtility.UrlEncode(sLong)); | |
try | |
{ | |
string xmlRestNearBy = GetUrl(sFindRestaurentByNearbySearch); | |
//create dataset to get list of restaurant | |
//XmlDocument xrestaurentNearBy = new XmlDocument(); | |
//xrestaurentNearBy.LoadXml(xmlRestNearBy); | |
//XmlNamespaceManager xrestaurentmanagerNearBy = new XmlNamespaceManager(xrestaurentNearBy.NameTable); | |
} | |
catch { } | |
#endregion | |
} | |
else | |
{ | |
/*-------------------Text Search---------------------*/ | |
#region TextSearch | |
/* Url Format for request | |
* https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key= API key obtained from Google | |
* | |
* parameter - {0} - text search query with (type of place to search + in + address) e.g. "restaurants+in+Vashi+Navi Mumbai+India" | |
* */ | |
string sFindRestaurentByTextSearch = "https://maps.googleapis.com/maps/api/place/textsearch/xml?query={0}&sensor=false&key=" + _GoogleMapsKey; | |
string sSearch = "restaurants+in+" + query.Replace(",", "+"); | |
sFindRestaurentByTextSearch = String.Format(sFindRestaurentByTextSearch, sSearch); | |
XmlNode xmlRestaurent = null; | |
try | |
{ | |
string xmlRestTextSearch = GetUrl(sFindRestaurentByTextSearch); | |
XmlDocument xrestaurentTextSearch = new XmlDocument(); | |
xrestaurentTextSearch.LoadXml(xmlRestTextSearch); | |
XmlNamespaceManager xrestaurentmanagerTextSearch = new XmlNamespaceManager(xrestaurentTextSearch.NameTable); | |
} | |
catch { } | |
#endregion | |
} | |
return gl; | |
} | |
public static DataSet GetLocationDetails(string sReference) | |
{ | |
DataSet dsDetail = new DataSet(); | |
dsDetail = null; | |
#region Place Detail Search | |
/* | |
*Url Format https://maps.googleapis.com/maps/api/place/details/xml?key={0}&sensor=false&reference= reference detail obtained from the above search | |
key - api key | |
* sensor = false | |
* reference - data under reference tag from the result of above place search | |
*/ | |
// sReference = "CnRwAAAAc2V7YS7cuNrAPYUwPbgTH1k4Ez0w78n5ZTsZhXxKSYXTa7JaA2m_YgLQ3bYTXflfNzZnTbFlub89P7GqKVmfo9uNeAtE8sZ-_CE0Ldhm9G_7sMFEJoHnpANPUeXoZmBJMyFEQrdmTJ6iqlLPr5Y93RIQtg8zeXRDWBtSwHQaUMQiMxoUy-5x7VIsudBQubDv2HXUMms69Yg"; | |
string sFindPlaceDetailByReference = "https://maps.googleapis.com/maps/api/place/details/xml?key={0}&sensor=false&reference={1}"; | |
sFindPlaceDetailByReference = String.Format(sFindPlaceDetailByReference, _GoogleMapsKey, sReference); | |
try | |
{ | |
string xmlPlaceDetailSearch = GetUrl(sFindPlaceDetailByReference); | |
dsDetail = convertStringToDataTable(xmlPlaceDetailSearch); | |
} | |
catch { } | |
return dsDetail; | |
#endregion | |
} | |
public static Geolocation? ResolveAddress(string address, string city, string state, string postcode, string country) | |
{ | |
return ResolveAddress(address + "," + city + "," + state + "," + postcode + " " + country); | |
} | |
public static Geolocation? ResolveAddress(string city, string state, string country) | |
{ | |
return ResolveAddress(city + "," + state + "," + country); | |
} | |
/// <summary> | |
///Retrieve a Url via WebClient | |
/// </summary> | |
/// <param name="url"></param> | |
/// <returns></returns> | |
private static string GetUrl(string url) | |
{ | |
string result = string.Empty; | |
System.Net.WebClient Client = new WebClient(); | |
using (Stream strm = Client.OpenRead(url)) | |
{ | |
StreamReader sr = new StreamReader(strm); | |
result = sr.ReadToEnd(); | |
} | |
return result; | |
} | |
private static DataSet convertStringToDataTable(string xmlString) | |
{ | |
DataSet dataSet = new DataSet(); | |
StringReader stringReader = new StringReader(xmlString); | |
dataSet.ReadXml(stringReader); | |
// logClass obj = new logClass(); | |
//obj.MailSenderLogMessage1("gcoder"+dataSet.Tables.Count.ToString()); | |
//obj.MailSenderLogMessage1(xmlString); | |
return dataSet; | |
} | |
} | |
public struct Geolocation | |
{ | |
public decimal Lat; | |
public decimal Lon; | |
public Geolocation(decimal lat, decimal lon) | |
{ | |
Lat = lat; | |
Lon = lon; | |
} | |
public override string ToString() | |
{ | |
return "Latitude: " + Lat.ToString() + " Longitude: " + Lon.ToString(); | |
} | |
public string ToQueryString() | |
{ | |
return "+to:" + Lat + "%2B" + Lon; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment