0

My question has been discussed here, but I could not get my issue solved with the answers, perhaps because of my JavaScript small background.

I am in need to convert an online Json to a Geojson. My function getmyJson is doing that:

var lat=-15.78 //latitude variable
var long=-47.93 //longitude variable
 var data_url="http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+long+"&appid=111111111111111111111111111";


   function getmyJson(data_url) {
 return JSON.parse($.ajax({
     type: 'GET',
     url: data_url,
     dataType: 'json',
     global: false,
     async:false,
     success: function(data) {

         var outGeoJson = {}
     outGeoJson['properties'] = data
     outGeoJson['type']= "Feature"
     outGeoJson['geometry']= {"type": "Point", "coordinates":
    [data.coord.lon, data.coord.lat]}

    var brasilia = L.geoJson(outGeoJson, { 
     onEachFeature: function (feature, layer)   
            {
            layer.bindPopup("N&#250;mero da RN: "+feature.properties.id+"<br>"+
                            "Localiza&#231;&#227;o: "+feature.properties.name+"<br>"+
                            "longitude: "+feature.geometry.coordinates[0]+"<br>"+
                            "latitude: "+feature.geometry.coordinates[1]
                            );//just to show something in the popup. could be part of the geojson as well!


        }

}).addTo(map);

    return outGeoJson;
     }
 }).responseText);

}
var myweather = getmyJson(data_url);

As you can see, I need: 1 - to call the getJson function passing the url [getmyJson(data_url); 2 - convert the Json to GeoJson (var outGeoJson); 3 - to assign the GeoJson to myweather variable ]. Everything works fine. However if I move the code 1 bellow right after the line var myweather = getmyJson(data_url); I get the following error:

Invalid GeoJson Object

code 1

var brasilia = L.geoJson(myweather, { 
     onEachFeature: function (feature, layer)   
            {
            layer.bindPopup("N&#250;mero da RN: "+feature.properties.id+"<br>"+
                            "Localiza&#231;&#227;o: "+feature.properties.name+"<br>"+
                            "longitude: "+feature.geometry.coordinates[0]+"<br>"+
                            "latitude: "+feature.geometry.coordinates[1]
                            );//just to show something in the popup. could be part of the geojson as well!


        }

}).addTo(map);

Could someone help me to figure out why I am getting this Error?

Thank you for your time and attention in advance

HelpOverFlow
  • 315
  • 1
  • 2
  • 10
  • Are you getting an error? Please explain the problem. – styfle Oct 09 '17 at 15:16
  • Hi @styfle. I did some editing in my previous message. The Error is **Invalid GeoJson object**. – HelpOverFlow Oct 09 '17 at 15:47
  • Related: https://stackoverflow.com/q/35870567/266535 – styfle Oct 09 '17 at 16:33
  • Related: https://stackoverflow.com/q/25575466/266535 – styfle Oct 09 '17 at 16:33
  • Do a `console.log(JSON.stringify(myweather))` and you will see the problem for your last error. The previous code sample is the correct way of using asynchronous operations. If you want to decouple code, you can wrap in functions or use Promises. – ghybs Oct 10 '17 at 02:32
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ghybs Oct 10 '17 at 02:33

0 Answers0