在 React 中将数据从一个 window 传递到另一个

Pass data from one window to another in React

大家好我有以下代码来获取我需要使用这些值将它们发送到另一个页面或组件的位置如果有人可以帮助我那将是非常有用的问候

      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();
    }

您似乎正在尝试使用 Google Geolocation API

下面是一个用于从该端点获取数据的异步函数示例:

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
}