Example #1
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var apiKey      = _config["Keys:GoogleKey"];
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"https://maps.googleapis.com/maps/api/geocode/json?address={encodedName}&key={apiKey}";

            var client = new HttpClient();

            var json = await client.GetStringAsync(url);


            var results  = JObject.Parse(json);
            var location = results["results"][0]["geometry"]["location"];
            var status   = results["status"];

            if (!location.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                result.Latitude  = (double)location["lat"];
                result.Longitude = (double)location["lng"];
                result.Success   = true;
                result.Message   = status.ToString();
            }

            return(result);
        }
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)  // generate GeoCoordResult in new file. move from root to Services dir
        {
            // make an instance of the result class and set some default values in case of a failure
            // when successful, just change the props
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            // Get Bing Maps service to convert names to long/lat (www.bingmapsportal.com)
            var apiKey      = _config["Keys:BingKey"];    // in Window Enviro Vari it is a double under score but can still use colon in config object
            var encodedName = WebUtility.UrlEncode(name); // need because going to generate this as an url as a uri

            // Build a URL ( a service address) that will get our coordinbates for us based on name we pass in
            // from Bing dev. Takes 2 params: 1 is our encodedName and 2 is a key, our apiKey
            var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            // This comes the resources folder of the project demo files

            // Create a client for use
            var client = new HttpClient();

            // Calls GetStringAsync(url) on the url to get th eresults of this query for the name
            var json = await client.GetStringAsync(url);  // return value is in json

            // Read out the results
            // Fragile, might need to change if the Bing API changes
            var results   = JObject.Parse(json);                     //parse with .Net json parser.  Allows us to walk through it and interogate it
            var resources = results["resourceSets"][0]["resources"]; // look for the resources we searched for

            if (!results["resourceSets"][0]["resources"].HasValues)  // if failed to return
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else  // if returned do a test with a prop called confidence. Determines how sure it is that the lat/long are correct.
            // if low we choose not to trust it
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' as a location";
                }
                else  // if found and confidence is high then set coords (a JToken)
                {
                    var coords = resources[0]["geocodePoints"][0]["coordinates"];
                    // set the result properties
                    result.Latitude  = (double)coords[0];
                    result.Longitude = (double)coords[1];
                    result.Success   = true;
                    result.Message   = "Success";
                }
            }
            return(result);
        }
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            await Task.Delay(1);

            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coors",
            };

            var apiKey = _config["Keys:BingKey"];

            _logger.LogInformation($"getting coordinates from bing map where I have a key {apiKey}");

            //            var encodedName = WebUtility.UrlEncode(name);
            //            var url = $"http://dev.virtualearth/REST/v1/Locations?q={encodedName}&key={apiKey}";
            //
            //            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 (!results["resourceSets"][0]["resources"].HasValues)
            //            {
            //                result.Message = $"Could not find '{name}' as a location";
            //            }
            //            else
            //            {
            //                var confidence = (string)resources[0]["confidence"];
            //                if (confidence != "High")
            //                {
            //                    result.Message = $"Could not find a confident match for '{name}' 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;
            //                }
            //            }

            result.Success   = true;
            result.Message   = "Success";
            result.Latitude  = 32.5141231;
            result.Longitude = -78.79787079;

            return(result);
        }
Example #4
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            // Create instance of GeoCoordResult with default values
            // Can easily return in case of a failure
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            // Info needed to make calls to bingMaps to get the long and lat
            var apiKey      = _config["Keys:BingKey"]; // Access our BingKey
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            // Create a new HttpClient
            var client = new HttpClient();

            // Calls the GetStrinAsync on that client using our ulr created above. Returns some JSON
            var json = await client.GetStringAsync(url);

            // Parses the results from the call above
            // 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 (!results["resourceSets"][0]["resources"].HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' as a location";
                }
                else
                {
                    // If we find a successful match then set the long and lat and udpate our success and message vars
                    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);
        }
Example #5
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var apiKey      = _config["Keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";


            /*
             * Sending Http-request to URL. Parsing the response into json.
             *
             * All credit to Shawn Wildermuth:
             * https://github.com/shawnwildermuth/BuildingWebASPNETCore/blob/master/BingMapParse.cs
             */

            var client = new HttpClient();

            var json = await client.GetStringAsync(url);      // Sending the request to our URL and attempting to get the response as a string.

            var results   = JObject.Parse(json);              // Parse the resulting string into a JSON-Object
            var resources = results["resourceSets"][0]["resources"];

            if (!resources.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' 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 <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get Coordinates"
            };

            var apiKey = _config["Keys:BingKey"];

            if (_env.IsEnvironment("Testing") || _env.IsEnvironment("Production"))
            {
                apiKey = Environment.GetEnvironmentVariable("BING_KEY");
            }

            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations/?q={encodedName}&key={apiKey}";

            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 (!results["resourceSets"][0]["resources"].HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' 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);
        }
Example #7
0
        //GeoCoordsResult new type of structure.
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            //Service Bing Maps to convert a name of a place, or address of a place into set of longitudes and latitudes. www.bingmapsportal.com
            var apiKey      = _config["keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(name);
            // construct the actual URL that the Bing Documentation gave it to us. we will take this URL across the web to parse some data to get the longitude and latitude.
            var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            // Actual calling to this URL. Get the Resource file from github.com/shawnwildermuth/BuildingWebASPNETCore

            var client = new HttpClient();

            var json = await client.GetStringAsync(url); // get the results of this query for the name.

            // Read out the results
            // Fragile, might need to change if the Bing API changes
            var results   = JObject.Parse(json);                        // parse with Linq to Json.
            var resources = results["resourceSets"][0]["resources"];

            if (!resources.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];   // what confidence the data returned is for the location we specified.
                if (confidence != "High")                              // we couldn't get a confident match for that name.
                {
                    result.Message = $"Could not find a confident match for '{name}' 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);
        }
Example #8
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string location) //we need to supply the name we want to get the coords for
        {
            var result = new GeoCoordsResult()                              //we set a default failed result so we can change it if we get the reliable coords
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var apiKey      = _config["Keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(location);
            var url         = $"http://dev.virtualearth.net/REST/v1/locations?q={encodedName}&key={apiKey}";

            // Read out the results


            // Fragile, might need to change if the Bing API changes

            var client = new HttpClient();

            var json = await client.GetStringAsync(url); //here we get the results of the url query (above) for the name

            var results   = JObject.Parse(json);         //we take the created jason and parse it with linq to json. the Jobject is who parses it
            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") //if we are not confident that geocoords are reliable, we will return an error
                {
                    result.Message = $"Could not find a confident match for '{location}' as a location";
                }
                else
                {
                    var coords = resources[0]["geocodePoints"][0]["coordinates"]; //if we get it we will get the coordinate
                    result.Latitude  = (double)coords[0];
                    result.Longitude = (double)coords[1];
                    result.Success   = true;
                    result.Message   = "Success";
                }
            }

            return(result);
        }
Example #9
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed To Get Coordinates"
            };


            var apiKey      = _config["Keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            //Read out the results
            // fragile, might need to be changed if the api changes
            var client = new HttpClient();

            var json = await client.GetStringAsync(url);

            var results = JObject.Parse(json);

            var resources = results["resourceSets"][0]["resources"];

            if (!results["resourceSets"][0]["resources"].HasValues)
            {
                result.Message = $"Could not find {name} as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];

                if (confidence != "High")
                {
                    result.Message = $"Could not find {name} 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);
        }
Example #10
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var apiKey      = _config["Keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(name);
            //Bing Map
            var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            //Creates client
            var client = new HttpClient();
            //Get results of query
            var json = await client.GetStringAsync(url);

            var results   = JObject.Parse(json);
            var resources = results["resourceSets"][0]["resources"];

            if (!results["resourceSets"][0]["resources"].HasValues)
            {
                //Could not find place name
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                //If confidence is not high
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' as a location";
                }
                else
                {
                    //Return coordinates
                    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);
        }
Example #11
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            GeoCoordsResult result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            string apiKey      = _config["Keys:BingKey"];
            string encodedName = WebUtility.UrlEncode(name);
            string url         = $"http://dev.virtualearth.net/REST/v1/Locations/?q={encodedName}&key={apiKey}";

            HttpClient client = new HttpClient();

            string json = await client.GetStringAsync(url);

            // Read out the results
            // Fragile, might need to change if the Bing API changes
            JObject results   = JObject.Parse(json);
            JToken  resources = results["resourceSets"][0]["resources"];

            if (!resources.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                string confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' as a location";
                }
                else
                {
                    JToken coords = resources[0]["geocodePoints"][0]["coordinates"];
                    result.Latitude  = (double)coords[0];
                    result.Longitude = (double)coords[1];
                    result.Success   = true;
                    result.Message   = "Success";
                }
            }

            return(result);
        }
Example #12
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var apiKey      = "AgKud4pI7wcW-i2Tk7zOspFU1nnBsfwdXRDaK-TWVOtWZA9Xka7iKrldCQwCAh78"; //_config["Keys:BingKey"];
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            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 (!results["resourceSets"][0]["resources"].HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' 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 <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            // var apiKey = _config["Keys:BingKey"];
            var apiKey      = "Agg74-2K2P4TtQ4Ob1aur_OqYpySZXeuiM_UMKpQRlCLKTez1PqkXdlBzK7sAV2K";
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            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 (!results["resourceSets"][0]["resources"].HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{name}' 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 <GeoCoordsResult> GetCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://maps.googleapis.com/maps/api/geocode/json?address={encodedName}&sensor=true";

            var client = new HttpClient();

            var json = await client.GetStringAsync(url);

            // Read out the results
            // Fragile, might need to change if the Google API changes
            var results = JObject.Parse(json);

            var gResults = results["results"][0]["geometry"];

            if (!gResults.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                var formattedAddress = results["results"][0]["formatted_address"];

                var location = gResults["location"];
                var lat      = location["lat"];
                var lng      = location["lng"];

                result.Latitude  = (double)lat;
                result.Longitude = (double)lng;
                result.Success   = true;
                result.Message   = "Success";

                result.StopName = formattedAddress != null?formattedAddress.ToString() : name;
            }

            return(result);
        }
Example #15
0
        public async Task <GeoCoordsResult> GeoCoordsAsync(string name)
        {
            var result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinate"
            };

            var apiKey      = "At0i3qW6lxpWVHq-H7gJTPR2-5eidlzq-qA_13NPoTByvuETmKx9DobCdUTJ6ct3";
            var encodedName = WebUtility.UrlEncode(name);
            var url         = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={apiKey}";

            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 found a coords";
            }
            else
            {
                var confidence = (string)resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident";
                }
                else
                {
                    var coords = resources[0]["geocodePoints"][0]["coordinates"];
                    result.Lat     = (double)coords[0];
                    result.Long    = (double)coords[1];
                    result.Success = true;
                    result.Message = "Success";
                }
            }

            return(result);
        }
Example #16
0
        public async Task <GeoCoordsResult> GetCoordsAsync(string name)
        {
            GeoCoordsResult result = new GeoCoordsResult()
            {
                Success = false,
                Message = "Failed to get coordinates"
            };

            name = name.Replace(" ", "+");
            string encodedName = WebUtility.UrlEncode(name);

            string url = $"http://maps.googleapis.com/maps/api/geocode/json?address={encodedName}&sensor=true_or_false";

            HttpClient client = new HttpClient();

            string json = await client.GetStringAsync(url);

            JObject results = JObject.Parse(json);

            JToken geometry = results["results"][0]["geometry"];

            if (!geometry.HasValues)
            {
                result.Message = $"Could not find '{name}' as a location";
            }
            else
            {
                JToken coords = geometry["location"];
                result.Latitude  = (double)coords["lat"];
                result.Longitude = (double)coords["lng"];
                result.Success   = true;
                result.Message   = "Success";
            }

            return(result);
        }