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úmero da RN: "+feature.properties.id+"<br>"+
"Localizaçã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úmero da RN: "+feature.properties.id+"<br>"+
"Localizaçã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