Home > front end >  Pass data from one window to another in React
Pass data from one window to another in React

Time:02-10

Hello everyone I have the following code to take the location I need to use those values to send them to another page or component if someone could help me it would be very useful greetings

      const processManualLocation = () => {
        const  url = `https://www.googleapis.com/geolocation/v1/geolocate?key=.........`;
        const http = new XMLHttpRequest();
        http.open("POST", url);
        http.onreadystatechange = function(){
          if(this.readyState == 4 && this.status == 200){
           let resultado = JSON.parse(this.responseText);
           let latitude = resultado.location.lat;
           let longitude = resultado.location.lng;
           console.log(latitude, longitude);
          }
        }
        http.send();
    }

CodePudding user response:

It seems like you're trying to use the Google Geolocation API.

Here's an example of a asynchronous function for fetching data from that endpoint:

TS Playground

// Ref: https://developers.google.com/maps/documentation/geolocation/overview

const API_KEY = `Your actual API key`;

async function processManualLocation () {
  const  url = `https://www.googleapis.com/geolocation/v1/geolocate?key=${API_KEY}`;
  const request = new Request(url, {method: 'POST'});
  const response = await fetch(request);
  if (!response.ok) throw new Error('Response not OK');
  const data = await response.json();
  const {accuracy, location: {lat, lng}} = data;
  const result = {accuracy, lat, lng};
  console.log(result);
  return result; // or whatever want to return from the response data
}
  •  Tags:  
  • Related