1

I got a problem when I try to get the json from an URL in my reverse geocoding method : I tried this based on a solution found on stackoverflow. When I try to show the json in my alert it shows: undefined

adressReverseGeoCode(item: any, elementId: any) {
var getJSON = (url: any, callback: any) => {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.responseType = 'json';
  xhr.onload = () => {
    var status = xhr.status;
    if (status == 200) {
      callback(null, xhr.response);
    } else {
      callback(status);
    }
  };
  xhr.send();
};

getJSON('http://nominatim.openstreetmap.org/reverse?format=json&lat=' + item.latitude + '&' + 'lon=' + item.longitude + '&addressdetails=1',
  (err: any, data: any) => {
    if (err != null) {
      alert('Something went wrong: ' + err);
    } else {
      alert(data.result);
    }

  });

}

I want to get the "display_name" from the json in order to put it in a text input later. You can try this link to see the json file

{
 "place_id":"154253419",
 "licence":"Data © OpenStreetMap contributors, ODbL 1.0. http:\/\/www.openstreetmap.org\/copyright",
 "osm_type":"way",
 "osm_id":"424211755",
 "lat":"-23.56183",
 "lon":"-46.6598392",
 "display_name":"Alameda Ministro Rocha Azevedo, Jardim Paulista, São Paulo, Microrregião de São Paulo, RMSP, Mesorregião Metropolitana de São Paulo, São Paulo, Southeast Region, 01410-001, Brazil",
 "address":{"road":"Alameda Ministro Rocha Azevedo","suburb":"Jardim Paulista","city_district":"Jardim Paulista","city":"São Paulo","county":"Microrregião de São Paulo","state_district":"Mesorregião Metropolitana de São Paulo","state":"São Paulo","postcode":"01410-001","country":"Brazil","country_code":"br"},
 "boundingbox":["-23.5642064","-23.5601209","-46.662319","-46.6580485"]
}

Thank you in advance for any help you can give me !

2
  • 1
    There is not data.result! thats why it is undefined. Your data is the JSON. so just do data.display_name Commented Mar 4, 2017 at 15:37
  • 2
    Also have to parse the json string to object. Commented Mar 4, 2017 at 15:39

1 Answer 1

1

There is not data.result! thats why it is undefined. Your data is the JSON. so just do data.display_name. Probably!

var obj = JSON.parse(data); // as suggested by @charlietfl
console.log(obj.display_name);
alert(obj.display_name);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will accept your answer in 4minutes once the cooldown time ends ^^

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.