0

I am trying unsuccessfully to extract the formatted_address property.

The following web service logs the JSON below to the console. I cannot get the formatted address using returnedData.d.results[0].formatted_address.

 $.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(returnedData);
        }
    });

The format of the json is the exact same as the format over here at Google.

Edit Darin pointed out that I was contradicting myself: the web service wraps up everything in the link above in a d object, I failed to mention that.

enter image description here

Further edit Here is the web service:

[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());

                return reader.ReadToEnd();
            }
        }

and here is the javascript:

/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)
        {
            alert(returnedData.d[0].results[0].formatted_address);
            console.log(returnedData);
        }
    });
8
  • 1
    Are you sure that there is a .d property in the result? Commented Jun 28, 2012 at 13:32
  • yes, it is an ASP.Net web service: see encosia.com/a-breaking-change-between-versions-of-aspnet-ajax Commented Jun 28, 2012 at 13:35
  • I know this. It's just that you are contradicting yourself in the question when you are saying that the JSON looks exactly the same as the link you have shown. So are you getting an error when you try to access returnedData.d.results[0].formatted_address? Commented Jun 28, 2012 at 13:35
  • What error are you getting in your console? Commented Jun 28, 2012 at 13:46
  • @Tamil If i use returnedData.d.results, I then get a results is 'undefined' error Commented Jun 28, 2012 at 13:47

3 Answers 3

1

Have you tried using returnedData.results[0].formatted_address without the .d. node. That does not exist!

Sign up to request clarification or add additional context in comments.

Comments

0

remove the ".d"

returnedData.results[0].formatted_address

but doing this you are always getting the first node only

1 Comment

what do you get if you do : console.log(returnedData.d); ?
0

The answer to this issue turns out to be that the web service is returning a string, instead of json. The built-in javascript serializer does not get used. The eval keyword or something more secure needs to be used. As it happens I ended up using the Google Maps Javascript API and that was much easier anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.