1

다음 코드는 비동기 적으로 작동합니다. 나는 도시, 국가를 위도와 경도로 변환하도록 설정했을뿐입니다. 그런 다음 그 값이 경고됩니다.Google의 GeoCode Convert City, STATE 위도 경도로

어떻게 실제로이 값을 반환하는 함수를 작성할 수 있습니까?

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

답변

1

콜백에서 값을 반환하면 아무 것도 수행하지 않습니다. 이러한 값을 어딘가에 저장하려면 콜백에서 수행하십시오. 위 코드에서 위도와 경도를 선언하면지도와 지오 코더 바를 선언 할 수 있습니다.

+0

정확히 내가 먼저 시도한 것이지만 양식 제출시 완료되고 시간 값이 설정되지 않습니다. – pws5068

1

아니요, Google지도 API는 동기식 지오 코딩 요청을 지원하지 않습니다. 그러나 일반적으로 동기식 요청을 사용하면 안됩니다. 이는 브라우저 UI가 응답을 수신 할 때까지 차단되어 사용자 경험이 끔찍할 수 있기 때문입니다.

+0

감사합니다. 대답은 두려웠다. 이러한 값을 저장하는 아약스 접근 방식을 고안하려고합니다. – pws5068

0

이 해결책을 얻으려면 Sharmil Desai의 "Google지도 지오 코더 용 .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; 
       } 
      } 
     } 
    } 
} 
관련 문제