0

I already am using a geolocation call from FreeCodeCamp I have a linked OpenWeatherMap API to find weather based on Geolocation I have now added a Google API so I can add not just the City name but also State and Country.

I cannot figure out or find how to use already determined geolocation data to get the info I need from Google GeoCoder API... All I can find on google is to create a new call which is redundant or I am reading wrong or I am not using the correct search terms in google because I am not finding anything...

I am only wanting to pull City, State, Country using Longitude and Latitude coordinates I already have. (I can get City and Country from Open Weather but not State or at least it isn't displaying for me) I am trying to not have to rewrite:

$(document).ready(function (){
var longi;
var lat;
var temp;

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
longi = position.coords.longitude;
lat = position.coords.latitude;

// create API with Geolocation
var api = "http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+longi+"&appid=API_GOES_HERE";  

var googGeoloc = "https://maps.googleapis.com/maps/api/js?key=API_GOES_HERE&callback=initMap";  

var geocoder = new google.maps.Geocoder;

//$(".location").html(lat + ", " + longi);
$.getJSON(api, function (data) {
//var weatherType = data.weather[0].description;
var ktemp = data.main.temp;
var fTemp = Math.round((ktemp)*(9/5)-459.67); //temp in F
var cTemp = Math.round((fTemp - 32) * (5/9)); //temp in c
var windSpeed = data.wind.speed;
var city = data.name;

$(".location").html(city);
$(".degrees").html("Your Current Temp is: " + fTemp + " Degrees");
});
})
}
});

As you can see from my code I already am grabbing GeoLocation coordinates... I want to use the longitude and latitude info I already have to get either just the State or City, State, Country from the Google API call... I can parse the City and Country from OpenWeatherMap and have on my test page... I just need the State/Province

1 Answers1

0

https://openweathermap.org/current#geo

their lat and long api does provides city just country as you can see from the returned json object,

`

api.openweathermap.org/data/2.5/weather?lat=35&lon=139

{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}

`

  • if you need help parsing the information I don't mind at all but you should be doing it as part of the learning process :) If you have any other questions let me know – Evil Dr. PorkChop May 30 '17 at 22:48
  • 1
    it just so happens the state is not provided on their rest api :) – Evil Dr. PorkChop May 30 '17 at 22:50
  • hmm I don't think the weather's api service provides the information you're looking for you may have to request a different api that can use your lat and long to determine the state/providence :) Maybe also try a request to the google api, not sure if their api returns the state/providence but I imagine so it's google maps hahaha :) You'll probably have to use promises to retrieve both data in a synchronous manner :) or w.e method you like to get data synching hehe – Evil Dr. PorkChop Jun 01 '17 at 02:42
  • try this once you have a google api key `https://maps.googleapis.com/maps/api/geocode/json?latlng=${SEARCHING_lat},${SEARCHING_long}&key=${YOUR_GOOGLE_API_KEY} ` the response json from that object has a ... results[0].formatted_address which includes the full address of the location which you can parse using regex or string manipulation to get the state/providence :) GL!~ if you need help let me know. – Evil Dr. PorkChop Jun 01 '17 at 02:47
  • for example the following path of the object could return something like this, i just picked a random place in mexico, i love me some carnitas from michoacan :) results[0].formatted_address: "Unnamed Road, Michoacán, Mexico", <-- you can split it from an array by , and take just the 2nd part to get the state/providence name hehe – Evil Dr. PorkChop Jun 01 '17 at 02:51