3

Google マップ ジオコーディング API を呼び出して、緯度と経度のペアから書式設定された住所を取得し、それをコンソールに記録しようとしています。特定の場所に対して返される最初の「formatted_address」アイテムを取得しようとしています。JSONからそのアイテムを抽出できないだけです。理由がわかりません。データを抽出するために必要なコード行は大歓迎です。

ジャバスクリプト:

//Gets the current location of the user
function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }

}

function showPosition(position)
{
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    $.ajax({
        type: "POST",
        url: "ReportIncident.aspx/ReverseGeocode",
        data: "{latitude:" + latitude + ",longitude:" + longitude + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (returnedData)
        {
         // console.log(/****formatted_address Here Please****/);
        }
    });
}

C#:

        [WebMethod]
        public static string ReverseGeocode(decimal latitude, decimal longitude)
        {
            // Create the web request  

            string url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude +
                         "&sensor=true";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Console application output  
                return reader.ReadToEnd();
            }
        }
4

2 に答える 2

2

から文字列を返しているため、JSON はエスケープされてWebMethodおり、ビルトインにそれを行わせていJavascriptSerializerません。

おおよそ、あなたはこれを見ています:

"\{ /*data*/ \}"

これの代わりに:

{ /*data*/ }

返された文字列を実行することでこれを修正できますが、推奨していないことに注意してください...できるeval()と言っているだけです

編集

 $.ajax({
    type: "POST",
    url: "ReportIncident.aspx/ReverseGeocode",
    data: "{latitude:" + latitude + ",longitude:" + longitude + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (returnedData)
    {
        var realReturnedData = eval(returnedData.d);
        //realReturnedData now has the google maps data structure you're expecting
    }
});

繰り返しますが... eval は悪です (ソースを 100% 信頼しない場合)。webmethod とは異なるデータ型を返すことをお勧めします...

編集2

[WebMethod]
public static MyClass ReverseGeocode(decimal lat, decimal long) {
   MyClass obj = new MyClass();
   //do stuff
   return obj;
}

しかし、私の本当の質問は、なぜサーバーへの Web サービス呼び出しでこれを行うのですか? Javascript から Google の API にジオコーディングを直接行うことができますhttps://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-reverse

于 2012-07-03T16:01:00.243 に答える
0

APIは次のようなものを返すと思います:

{
    "results" : [
    {
        "address_components" : [/***/],
        "formatted_address": "..."
        /* other */
    }],
    "status" : "OK"
}

あなたは試すことができます:

if(returnedData.results.length > 0) {
    console.log(returnedData.results[0].formatted_address);
}

編集: API ドキュメントをご覧ください https://developers.google.com/maps/documentation/geocoding/

于 2012-07-03T15:50:03.213 に答える