protected LocationDetailsQuery QueryGoogleForLocation(string locstr) { System.IO.Stream Str = null; System.IO.StreamReader srRead = null; string retStr = null; try { // make a Web request System.Type t = this.GetType(); string s = "http://maps.google.com/maps/api/geocode/xml?address=" + locstr.Replace(" ", "%20") + "&sensor=false"; System.Net.WebRequest req = System.Net.WebRequest.Create(s); System.Net.WebResponse resp = req.GetResponse(); Str = resp.GetResponseStream(); srRead = new System.IO.StreamReader(Str); // read all the text retStr = srRead.ReadToEnd(); } catch (Exception ex) { throw new Exception("Couldnt Load Data From Google : " + ex.ToString()); retStr = null; } finally { // Close Stream and StreamReader when done if (srRead != null) { srRead.Close(); } if (Str != null) { Str.Close(); } } LocationDetailsQuery details = new LocationDetailsQuery(); if (!(ReadResult(retStr) == "OK")) { details.ErrorCode = "Error Getting Status Code"; } else { details.LongLat = ReadLong(retStr) + ", " + ReadLat(retStr); details.PCode = ReadAddressComponent(retStr, "postal_code"); details.Country = ReadAddressComponent(retStr, "country", true); } return(details); }
public LongLat QueryGoogleForLongLat(GoogleMapAddress add) { if (add == null) { throw new NullReferenceException("Invalid LongLat Object. Cannot Load PostCode"); } if (add.PostCode.Split(' ').Count() > 2) { throw new Exception("Invalid Postcode Format"); } LongLat retVal; try { retVal = QueryLocalSourceForLongLat(add); } catch (Exception e) { retVal = null; } if (retVal == null) { try { LocationDetailsQuery qr = QueryGoogleForLocation(add.ToString()); if (qr.ErrorCode != null) { throw new Exception("Error Loading Location Details : " + qr.ErrorCode); } else if (qr.LongLat == null) { throw new PostCodeNotFoundException("Post Code Not Found For (" + add.PostCode + "," + add.Country + ")"); } retVal = new LongLat(qr.LongLat.Split(',')[0], qr.LongLat.Split(',')[1], add.PostCode); } catch (Exception ex) { throw new Exception("Failed to get location details"); } } return(retVal); }
public GoogleMapAddress QueryGoogleForAddress(LongLat ll) { // if (Add == null) // throw new NullReferenceException("Invalid Address Object. Cannot Load LongLat"); GoogleMapAddress retVal; try { retVal = QueryLocalSourceForAddress(ll); } catch (Exception ex) { retVal = null; } if (retVal == null) { try { LocationDetailsQuery qr = QueryGoogleForLocation(ll.ToString()); if (qr.ErrorCode != null) { throw new Exception("Error Loading Location Details : " + qr.ErrorCode); } else if (qr.PCode == null) { throw new PostCodeNotFoundException("Post Code Not Found For (" + ll.Longatude + ", " + ll.Latatude + ")"); } else if (qr.Country == null) { throw new CountryNotFoundException("Country Not Found For (" + ll.Longatude + ", " + ll.Latatude + ")"); } retVal = new GoogleMapAddress(qr.PCode, qr.Country); } catch (Exception ex) { throw new Exception("Failed to get location details"); } } return(retVal); }