GeocoderLocation IGeoCoder.Geocode(PropertyLocation address) { string queryAddress = string.Format(((IGeoCoder)this).UrlPattern, HttpUtility.UrlEncode(address.ToString())); if (!string.IsNullOrWhiteSpace(address.ZipCode)) { queryAddress = string.Format("{0}&components=postal_code:{1}|country:DE", queryAddress, address.ZipCode); } WebRequest request = WebRequest.Create(queryAddress); using (WebResponse response = request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { XDocument document = XDocument.Load(stream); XElement longitudeElement = document.Descendants("lng").FirstOrDefault(); XElement latitudeElement = document.Descendants("lat").FirstOrDefault(); if (longitudeElement != null && latitudeElement != null) { return new GeocoderLocation() { Longitude = double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture), Latitude = double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture) }; } } } return null; }
GeocoderLocation IGeoCoder.Geocode(PropertyLocation address) { string queryAdress = string.Format(((IGeoCoder)this).UrlPattern, ((IGeoCoder)this).ApiKey, HttpUtility.UrlEncode(address.ToString())); WebRequest request = WebRequest.Create(queryAdress); using (WebResponse response = request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { XDocument document = XDocument.Load(stream); XElement longitudeElement = document.Descendants("lng").FirstOrDefault(); XElement latitudeElement = document.Descendants("lat").FirstOrDefault(); if (longitudeElement != null && latitudeElement != null) { return new GeocoderLocation() { Longitude = double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture), Latitude = double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture) }; } } } return null; }
private static string GetRouteAsStoredFileCore(PropertyLocation source, PropertyLocation destination) { int width = 800; int height = 800; // https://developers.google.com/maps/documentation/directions/?hl=de string originText = source.ToString(); string destinationText = destination.ToString(); StringBuilder sbInitialRequest = new StringBuilder(); sbInitialRequest.Append("http://maps.google.com/maps/api/directions/xml?"); sbInitialRequest.AppendFormat("origin={0}", HttpUtility.UrlEncode(originText)); sbInitialRequest.AppendFormat("&destination={0}", HttpUtility.UrlEncode(destinationText)); sbInitialRequest.Append("&sensor=false"); WebRequest wreqInitial = WebRequest.Create(sbInitialRequest.ToString()); XDocument docResponse = null; using (WebResponse wresInitial = wreqInitial.GetResponse()) { using (Stream stream = wresInitial.GetResponseStream()) { docResponse = XDocument.Load(stream); } string status = docResponse.Root.Element("status").Value; if (status != "OK") { Logger.Instance.LogFormat(LogType.Error, typeof(RoutePlanHelper), Properties.Resources.MapsRequestFailed, status); return null; } } XElement overviewE = docResponse.Root.Element("route").Element("overview_polyline").Element("points"); StringBuilder sbContinuationRequest = new StringBuilder(); sbContinuationRequest.Append("http://maps.google.com/maps/api/staticmap?"); sbContinuationRequest.AppendFormat("size={0}x{1}", width, height); sbContinuationRequest.AppendFormat("&sensor=false&path=weight:3|color:red|enc:{0}", overviewE.Value); WebRequest wr1 = WebRequest.Create(sbContinuationRequest.ToString()); using (WebResponse res1 = wr1.GetResponse()) { using (Image image = Image.FromStream(res1.GetResponseStream())) { using (FileStream tempFile = File.OpenWrite(Path.GetTempFileName())) { image.Save(tempFile, ImageFormat.Png); return tempFile.Name; } } } }
private static string GetRouteAsStoredFileCore(PropertyLocation source, PropertyLocation destination) { int width = 800; int height = 800; // https://developers.google.com/maps/documentation/directions/?hl=de string originText = source.ToString(); string destinationText = destination.ToString(); // Create initial request StringBuilder sbInitialRequest = new StringBuilder(); sbInitialRequest.Append("http://maps.google.com/maps/api/directions/xml?"); sbInitialRequest.AppendFormat("origin={0}", originText); sbInitialRequest.AppendFormat("&destination={0}", destinationText); sbInitialRequest.Append("&sensor=false"); WebRequest wreqInitial = WebRequest.Create(sbInitialRequest.ToString()); XDocument docResponse = null; using (WebResponse wresInitial = wreqInitial.GetResponse()) { docResponse = XDocument.Load(wresInitial.GetResponseStream()); // Load the response XML string status = docResponse.Root.Element("status").Value; switch (status) { // TODO: Handle the errors! case "NOT_FOUND": case "ZERO_RESULTS": Logger.Instance.LogFormat(LogType.Warning, typeof(RoutePlanHelper), "The maps-request failed with status '{0}'. This is an indication that the location could not be retrieved. Sorry, but there's no workaround.", status); return null; case "OVER_QUERY_LIMIT": Logger.Instance.LogFormat(LogType.Error, typeof(RoutePlanHelper), "The maps-request failed with status 'OVER_QUERY_LIMIT'. This indicates too many queries within a short timeframe."); return null; case "MAX_WAYPOINTS_EXCEEDED": case "INVALID_REQUEST": case "REQUEST_DENIED": case "UNKNOWN_ERROR": default: Logger.Instance.LogFormat(LogType.Error, typeof(RoutePlanHelper), "The maps-request failed with status '{0}'. Please contact the developers!", status); return null; case "OK": // Everything ok. break; } } // Get the path data XElement overviewE = docResponse.Root.Element("route").Element("overview_polyline").Element("points"); StringBuilder sbContinuationRequest = new StringBuilder(); sbContinuationRequest.Append("http://maps.google.com/maps/api/staticmap?"); sbContinuationRequest.AppendFormat("size={0}x{1}", width, height); // TODO: Maybe allow configuring the thickness and color, especially for b/w-printers? sbContinuationRequest.AppendFormat("&sensor=false&path=weight:3|color:red|enc:{0}", overviewE.Value); WebRequest wr1 = WebRequest.Create(sbContinuationRequest.ToString()); using (WebResponse res1 = wr1.GetResponse()) { using (Image image = Image.FromStream(res1.GetResponseStream())) { using (FileStream tempFile = File.OpenWrite(Path.GetTempFileName())) { image.Save(tempFile, ImageFormat.Png); return tempFile.Name; } } } }