/// <summary> /// Parse the Bing response, courtesy of the Pluralsight instructor /// </summary> /// <param name="json"></param> /// <param name="location"></param> /// <returns></returns> private CoordServiceResult parseResponse(string json, string location) { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failure because reasons" }; var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; return result; } var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; return result; } var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; return result; }
public async Task<CoordServiceResult> Lookup(string location, string country = "Netherlands") { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failure while looking up coordinates" }; var gmapsKey = Startup.Configuration["AppSettings:GmapsKey"]; var url = $"https://maps.googleapis.com/maps/api/geocode/json?components=locality:|administrative_area:{location}&key={gmapsKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); var results = JObject.Parse(json); var resources = results["results"][0]["geometry"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var coords = resources["location"]; result.Latitude = (double)coords["lat"]; result.Longitude = (double)coords["lng"]; result.Success = true; result.Message = "Success"; } return result; }
public async Task<CoordServiceResult> Lookup(string Location) { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failture while looking up coordinates" }; //look up coordinates string encodeName = WebUtility.UrlDecode(Location); //czytam bing Key z zmiennych środowiskowych żeby nie udosępniać w repozytorium :p string bingMapsKey = Startup.Configuration["AppSettings:BingKey"]; var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodeName}&key={bingMapsKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; var coord = resources[0]["geocodePoints"][0]["coordinates"]; result.Long = (double)coord[1]; result.Lat = (double)coord[0]; result.Success = true; result.Message = "Success"; return result; }
public async Task<CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failure while looking up coordinates" }; // Look Coordinates Bing map services.... var encodedLocation = System.Net.WebUtility.UrlEncode(location); var url = $"http://maps.googleapis.com/maps/api/geocode/json?address={encodedLocation}&sensor=true"; var client = new HttpClient(); var json = await client.GetStringAsync(url); var res = JObject.Parse(json); if (res["status"].Equals("ZERO_RESULTS")) { result.Message = "Location was not found."; return result; } if (res["status"].ToString() != "OK") { result.Message = "Unknown error!"; return result; } result.Latitude = (double)res["results"][0]["geometry"]["location"]["lat"]; result.Longitude = (double)res["results"][0]["geometry"]["location"]["lng"]; result.Message = "Success"; result.Success = true; return result; }
public async Task<CoordServiceResult> Lookup(string location) { try { var result = new CoordServiceResult() { Success = false, Message = "Failed lookup" }; //Lookup coordinates var encodedLocation = WebUtility.UrlEncode(location); var key = Startup.Configuration["AppSettings:BingKey"]; var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedLocation}&key={key}"; //system.net.http namespace for httpclient, this is a better compatibility with asp.net 5 var client = new HttpClient(); var json = await client.GetStringAsync(url); var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"No locations found for {location}"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for {location}"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Longitude = (double)coords[1]; result.Latitude = (double)coords[0]; result.Success = true; result.Message = "Success"; } } return result; } catch (Exception exc) { _logger.LogError($"Failed to lookup coordinates of location: {location}", exc); return null; } }
public async Task <CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Undertermined failure while looking up coordinates." }; // Lookup Coordinates var encodedName = WebUtility.UrlEncode(location); var bingKey = Startup.Configuration["AppSettings:BingKey"]; if (bingKey == null) { result.Message = "No BingKey found on system. Could not lookup the location."; return(result); } var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); // Read out the results // Fragile, might need to change if the Bing API changes var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } return(result); }
public async Task <CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Unkown failure looking up coordinates" }; var encodedName = WebUtility.UrlDecode(location); var bingKey = Startup.Configuration["TheWorld:BingKey"]; var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; result.Latitude = 25.774810791015625; result.Longitude = -80.1977310180664; result.Success = true; result.Message = "Success"; return(result); var client = new HttpClient(); var json = await client.GetStringAsync(url); // Read out the results // Fragile, might need to change if the Bing API changes var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } return(result); }
public async Task<CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Undertermined failure while looking up coordinates." }; // Lookup Coordinates var encodedName = WebUtility.UrlEncode(location); var bingKey = Startup.Configuration["AppSettings:BingKey"]; if (bingKey == null) { result.Message = "No BingKey found on system. Could not lookup the location."; return result; } var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); // Read out the results // Fragile, might need to change if the Bing API changes var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } return result; }
public async Task<CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Unkown failure looking up coordinates" }; var encodedName = WebUtility.UrlDecode(location); var bingKey = Startup.Configuration["TheWorld:BingKey"]; var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; result.Latitude = 25.774810791015625; result.Longitude = -80.1977310180664; result.Success = true; result.Message = "Success"; return result; var client = new HttpClient(); var json = await client.GetStringAsync(url); // Read out the results // Fragile, might need to change if the Bing API changes var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } return result; }
public async Task <CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult { Success = false, Message = "Undetermined failure while looking up coordinates" }; // Lookup coordinates var bingKey = Startup.Configuration["AppSettings:BingKey"]; var encodedName = WebUtility.UrlDecode(location); var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); #region Bing Parsing Code var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } #endregion return(result); }
public async Task<CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failure while looking up coordinates" }; //URL to create new bing maps key : http://www.microsoft.com/maps/create-a-bing-maps-key.aspx //BingMap portal url : https://www.bingmapsportal.com/Application# var bingKey = Startup.Configuration["AppSettings:BingKey"]; var encodedName = WebUtility.UrlEncode(location); var url = $"http://dev.virtualearth.net/REST/v1/locations?q={encodedName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); // Read out the results // Fragile, might need to change if the Bing API changes var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confident match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } return result; }
public async Task <CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failure while looking up coordinates" }; // Lookup Coordinates var encodedName = WebUtility.UrlEncode(location); var bingKey = Startup.Configuration["AppSettings:BingKey"]; var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); ParseResultFromMapApi(location, result, json); return(result); }
public async Task<CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult { Success = false, Message = "Undetermined" }; var encodeName = WebUtility.UrlEncode(location); var bingKey = Startup.Configuration["AppSettings:BingKey"]; var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodeName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); var results = JObject.Parse(json); var resources = results["resourceSets"][0]["resources"]; if (!resources.HasValues) { result.Message = $"Could not find '{location}' as a location"; } else { var confidence = (string)resources[0]["confidence"]; if (confidence != "High") { result.Message = $"Could not find a confidence match for '{location}' as a location"; } else { var coords = resources[0]["geocodePoints"][0]["coordinates"]; result.Latitude = (double)coords[0]; result.Longitude = (double)coords[1]; result.Success = true; result.Message = "Success"; } } return result; }
public async Task<CoordServiceResult> Lookup(string location) { var result = new CoordServiceResult() { Success = false, Message = "Undetermined failure while looking up coordinates" }; var encodedName = WebUtility.UrlEncode(location); var bingKey = Startup.Configuration["AppSettings:BingKey"]; // Look up coord var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}"; var client = new HttpClient(); var json = await client.GetStringAsync(url); // Would do the json parsing here .. return result; }