Reputation: 37
I first use this code to get my geo location:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
long = position.coords.longitude;
lat = position.coords.latitude;
$("#data").html("latitude: " + lat + "<br>longitude: " + long);
});
}
Then I use the geo location to call API:
var api = "http://api.openweathermap.org/data/2.5/weather?lat={+lat+}&lon={+long+}&appid=a85717f57b6bd30e011747de59dc3a60"
It returns code error 400, bad request.
Upvotes: 1
Views: 5654
Reputation: 805
I think the problem is that you have not properly concatenated the variables lat and long into the URL. Use " + lat + "
rather than {+lat+}
like this: var api = "http://api.openweathermap.org/data/2.5/weather?lat=" +lat+ "&lon=" +long+ "&appid=a85717f57b6bd30e011747de59dc3a60";
that works for me.
Another potential issue may be scope; if your var api
is outside of the if statement it won't know what the variables lat and long
are. One way around this is to declare those variables globally (outside the if statement). Or move the var api
inside the statement.
Upvotes: 2