谷歌的GeoCode转换城市,状态为纬度经度

时间:2010-07-14 13:26:30

标签: javascript asynchronous geocoding latitude-longitude google-geocoder

以下代码异步工作。我只是将它设置为将城市,州转换为纬度和经度。然后警告该值。

如何编写实际返回这些值的函数?

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 + " Longitude" + longitude);

  }
}

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

3 个答案:

答案 0 :(得分:1)

从回调中返回这些值不会做任何事情。如果你想将这些值存储在某个地方,只需在回调中执行。如果你只是在声明地图和地理编码器变量的顶部声明纬度和经度,你可以在当前代码中完成。

答案 1 :(得分:1)

不,据我所知,Google Maps API不支持同步地理编码请求。但是,通常您不应该使用同步请求。这是因为浏览器UI会在收到响应之前阻塞,这会产生糟糕的用户体验。

答案 2 :(得分:0)

对于此解决方案,您需要从CodeProject(开放许可证)下载Sharmil Desai的“用于Google Maps Geocoder的.NET API”,位于此处: http://www.codeproject.com/KB/custom-controls/GMapGeocoder.aspx

实施以下代码,插入所需的城市,州或街道地址,GoogleMapsAPI将使用GMapGeocoder的实用工具方法'Util.Geocode'返回您的GeoCoded结果。

快乐的编码!

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;
                }
            }
        }
    }
}