How to get the Latitude and Longitude from a address using the google geocode API?

Viewed 143

Creating a weather app to learn JavaScript. I have managed to get the weather from OpenWeatherMap API for the location of the computer when the pages loads based on latitude and longitude, Then I have a drop down box that autofills using google places API, then I have a submit button where I want to convert the name of the place into the latitude and longitude to get the weather from OpenWeatherMap for the new location. I can't figure out what to pull from the results.

First: the JSON data:

   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Tbilisi",
               "short_name" : "Tbilisi",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Didi digomi",
               "short_name" : "Didi digomi",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tbilisi",
               "short_name" : "Tbilisi",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Georgia",
               "short_name" : "GE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Tbilisi, Georgia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.8438937,
                  "lng" : 45.0176811
               },
               "southwest" : {
                  "lat" : 41.6210248,
                  "lng" : 44.6600246
               }
            },
            "location" : {
               "lat" : 41.7151377,
               "lng" : 44.827096
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.8438937,
                  "lng" : 45.0176811
               },
               "southwest" : {
                  "lat" : 41.6210248,
                  "lng" : 44.6600246
               }
            }
         },
         "place_id" : "ChIJa2JP5tcMREARo25X4u2E0GE",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
} 

then my code, where I am able to get the STATUS fine, but I can't figure out how to change it to get the Latitude and Longitude:


var request = new XMLHttpRequest();

                request.open('GET',link,true);
                request.onload = function(){
                var obj2 = JSON.parse(this.response);
                if (request.status >= 200 && request.status < 400) {

                var newLat = (obj2.status);
                document.write(obj2.status);
               // var newLng = (obj2.results.formatted_address);
                alert(newLat);

This results in an alert of "OK" and the document having "OK" being written on it, but when I try to change it to pull the latitude and longitude data, it freaks out.

Please help.

0 Answers
Related