Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

MapQuest API Fetch

https://developer.mapquest.com/documentation/geocoding-api/address/get


In Javascript, how to get  "latLng": {"lat": 38.892062,"lng": -77.019912}? I just need lat and lng.

and I tried to fetch. but it does not work.

Can you help?

(The key is not in public usage but you can click above link to generate json)


function FindGeoCode() {
         var apiUrl = 'https://www.mapquestapi.com/geocoding/v1/address?key=WOKB4fgsdfgfd6QrlGikgp25oTcLmFByeUHr6slI&location=Washington,DC';
            const response = await fetch(apiUrl);
         //const data = await response.json();
         alert("HI");
      }

Open in new window


Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

function FindGeoCode2() {

         //https://reqbin.com/code/javascript

         var apiUrl = 'https://www.mapquestapi.com/geocoding/v1/address?key=WOKB46QrlGidsfasdfkgp25oTcLmFByeUHr6slI&location=Washington,DC';

         fetch(apiUrl,

            {

                method: 'GET',

                headers: {

                    'Accept': 'application/json',

                },

            })

                .then(response => response.json())

                .then(response => alert(JSON.stringify(response)))                              

      }


Avatar of Michel Plungjan
If you get the same result as the demo on

https://developer.mapquest.com/documentation/geocoding-api/address/get

then the code is this

https://jsfiddle.net/mplungjan/s1qxbpcr/

const { lat, lng } = data.results[0].locations[0].displayLatLng;

console.log(lat,lng)

Open in new window

const data = JSON.stringify(response);

const dataParse = JSON.parse(data);               


I have to stringify & then parse to make it working.

Anyway I can make it better? When I just JSON.parse, it does not work
               

function FindGeoCode() {
            //https: reqbin.com / code / javascript
            var apiUrl = 'https://www.mapquestapi.com/geocoding/v1/address?key=WOKB46QrlGikgp25oTcLmFByeUHr6slI&location=Washington,DC';
         fetch(apiUrl,
                {
                    method: 'GET',
               headers:
               {
                        'Accept': 'application/json',
                    },
                })
            .then(response => response.json())


            .then(response => {
               const data = JSON.stringify(response);
               const dataParse = JSON.parse(data);               
               const { lat, lng } = dataParse.results[0].locations[0].displayLatLng;               
               alert("Found Your selected Geo, and we will redirect you " + lat + ' ' + lng);
                    location.href = "listing-Agency.html?lat="+lat+"&lng="+lng;
            }
            )
           }

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am very new about this. if you can teach me more, I will appreciate it
Sure, what do you need to know?
I assume the code I posted worked for you since you accepted the comment as answer?