开发者

Google's GeoCode Convert City, STATE into Latitude Longitude

开发者 https://www.devze.com 2023-01-06 22:43 出处:网络
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.

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 actually returns these values?

var map;
var geocoder;

function initialize() {

  geocoder = new GClientGeocoder();

}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  //map.clearOverlays();
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } else {
    place = response.Placemark[0];

    latitude = place.Point.coordinates[0];
    longitude = place.Point.coordinates[1];

    alert("Latitude " + latitude + " Lo开发者_运维问答ngitude" + longitude);

  }
}

function showLocation(address) {
  geocoder.getLocations(address, addAddressToMap);
}


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.


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.


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;
namespace GeoCodeAddresses
{
    class Program
    {
        static void Main(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;
                }
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消