It is necessary to get the coordinates to the address through the geocoder from JSON, and everything would be fine, but the received JSON does not want to parse, it gives an error. Moreover, if you go into the developer tools in Chrome and run the script manually, then everything works. Tell me, who knows how to solve the problem?

<script src="http://maps.api.2gis.ru/2.0/loader.js?pkg=full" data-id="dgLoader"></script> <script type="text/javascript"> var map; DG.then(function () { map = DG.map('map', { "center": [48.65626, 44.477077], "zoom": 10 }); var XHR = ("onload" in new XMLHttpRequest()) ? XMLHttpRequest : XDomainRequest; var meetings = document.getElementById('MeetingsInfo'); var meetingsCount = Number.parseInt(document.getElementById('MeetingsCount').innerText); for (var i = 0; i < meetingsCount; i++) { var req = new XHR(); req.open('GET', 'https://nominatim.openstreetmap.org/search?q=' + encodeURIComponent(meetings.rows[i+1].cells[0].innerText) + '&format=json'); req.send(null); if (req.responseText != null) { var info = JSON.parse(req.responseText);//здесь ошибка var place = info[0]; var lat = Number.parseFloat(place.lat); var lon = Number.parseFloat(place.lon); DG.marker([lat, lon]).addTo(map).bindPopup(meetings.rows[i + 1].cells[2].innerText); } } geoclicker = true; }); </script> 

Mistake

Debugging

UPD: made the request processing through onload, for the first record everything was processed, and the next one again was an error. At the same time, he also does not want to get the data from the table field, although manually enter image description here

  • an example of the request, and better answer the answer to the question - Stranger in the Q
  • Where do you expect a response from the API? Are you aware that the XMLHttpRequest request is asynchronous? - Stepan Kasyanenko
  • Listen to the previous speaker and add request processing to the req.onload function or req.onreadystatechange - Stranger in the Q
  • @StepanKasyanenko in front I am a lamer, so I can do a lot of bad things ) - Jaxx34
  • @ Jaxx34 already done. Google about AJAX and asynchronous programming. Find an example using XMLHttpRequest. - Stepan Kasyanenko

1 answer 1

It turned out that I was incorrectly handling an asynchronous request. So it all worked:

  <script type="text/javascript"> var map; DG.then(function () { map = DG.map('map', { "center": [48.65626, 44.477077], "zoom": 10 }); var XHR = ("onload" in new XMLHttpRequest()) ? XMLHttpRequest : XDomainRequest; var meetings = document.getElementById('MeetingsInfo'); var meetingsCount = Number.parseInt(document.getElementById('MeetingsCount').innerText); var meetingsArr = []; for (let i = 1; i < meetingsCount+1; i++) { meetingsArr.push(meetings.rows[i]); } meetingsArr.forEach(async (meeting) => { var req = new XHR(); await req.open('GET', 'https://nominatim.openstreetmap.org/search?q=' + encodeURIComponent(meeting.cells[0].innerText) + '&format=json'); req.onload = function() { if (req.responseText != null) { var info = JSON.parse(req.responseText); var place = info[0]; var lat = Number.parseFloat(place.lat); var lon = Number.parseFloat(place.lon); DG.marker([lat, lon]).addTo(map).bindPopup(meeting.cells[2].innerText); } }; req.send(null); } ); geoclicker = true; }); </script>