1

以下代码异步工作。我只设置了将城市,州转换为纬度和经度。然后会提醒该值。

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

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

3 回答 3

1

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

于 2010-07-14T13:30:32.997 回答
1

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

于 2010-07-14T13:31:20.390 回答
0

对于此解决方案,您需要从 CodeProject(开放许可证)下载 Sharmil Desai 的“A .NET API for the Google Maps Geocoder”,位于: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;
                }
            }
        }
    }
}
于 2010-09-20T05:49:50.397 回答