Skip to content Skip to sidebar Skip to footer

Google's Geocode Convert City, State Into Latitude Longitude

The following code works asynchronously. I only have it set up to convert a City, State into latitude and longitude. That value is then alerted. How can I write a function that a

Solution 1:

Return those values from the callback won't doing anything. If you want to store those values somewhere just do it in the callback. You could do it in your current code if you just declare latitude and longitude at the top where declare the map and geocoder vars.

Solution 2:

No, as far as I know, the Google Maps API does not support synchronous geocoding requests. However, in general you shouldn't be using synchronous requests. This is because the browser UI would block until it receives the response, and that would make a terrible user experience.

Solution 3:

For this solution, you will need to download Sharmil Desai's "A .NET API for the Google Maps Geocoder" from CodeProject (open license), located here: http://www.codeproject.com/KB/custom-controls/GMapGeocoder.aspx.

Implement the following code, inserting the required city, state, or street address, and the GoogleMapsAPI will return your GeoCoded results using the GMapGeocoder's utility method, 'Util.Geocode'.

Happy Coding!

using System;
using System.Collections.Generic;
using System.Linq;
using GMapGeocoder;
namespaceGeoCodeAddresses
{
    classProgram
    {
        staticvoidMain(string[] args)
        {
            string city = "Carmel";
            string state = "Indiana";
            string GMapsAPIkey = 
                System.Configuration.ConfigurationSettings.AppSettings["GoogleMapsApiKey"].ToString();

                GMapGeocoder.Containers.Results results =
                    GMapGeocoder.Util.Geocode(
                    string.Format("\"{1}, {2}\"", city, state), GMapsAPIkey);

                switch (results.StatusCode)
                {
                    case StatusCodeOptions.Success:
                        GMapGeocoder.Containers.USAddress match1 = results.Addresses[0];
                        //city = match1.City;//state = match1.StateCode;double lat = match1.Coordinates.Latitude;
                        double lon = match1.Coordinates.Longitude;
                        Console.WriteLine("Latitude: {0}, Longitude: {1}", lat, lon);
                        break;
                    case StatusCodeOptions.BadRequest:
                        break;
                    case StatusCodeOptions.ServerError:
                        break;
                    case StatusCodeOptions.MissingQueryOrAddress:
                        break;
                    case StatusCodeOptions.UnknownAddress:
                        break;
                    case StatusCodeOptions.UnavailableAddress:
                        break;
                    case StatusCodeOptions.UnknownDirections:
                        break;
                    case StatusCodeOptions.BadKey:
                        break;
                    case StatusCodeOptions.TooManyQueries:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}

Post a Comment for "Google's Geocode Convert City, State Into Latitude Longitude"