Я использую клиентскую библиотеку node.js от Google, которую можно найти здесь - https://github.com/googlemaps/google-maps-services-js, в моем компоненте React. Я пытаюсь использовать матрицу расстояний Google в этой функции:
googleMapsClient.distanceMatrix({
origins: "1002 Deming Way, Madison, WI 53717",
destinations: that.state.deliverycoords,
units: "imperial"
}, function(err, response){
if (err){
console.info("Error: ", err);
} else {
that.setState(
{
delivery: Object.assign({}, that.state.delivery, {
time: response.rows.elements[0].duration.text,
miles: response.rows.elements[0].distance.text,
deliveryFeeTotal:
parseInt(response.rows.elements[0].distance.text.split(" ")[0]) *
that.state.deliveryFeePerMile
})
},
function() {
console.info("Retrieved duration: ", that.state.delivery.time);
console.info("Retrieved miles:", that.state.delivery.miles);
console.info(
"Retrieved total delivery fee: ",
that.state.delivery.deliveryFeeTotal
);
}
);
}
});
that.state.deliverycoords динамичный. Когда я запускаю эту функцию, она делает запрос, консоль браузера возвращает это:
Failed to load https://maps.googleapis.com/maps/api/distancematrix/json?destinations=-94.12788%2C45.547585&origins=1002%20Deming%20Way%2C%20Madison%2C%20WI%2053717&units=imperial&key=AIzaSyCe43xuJivO4RphF2c5PgPWdkbxlFz4mL0: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://1zjr04npqj.codesandbox.io' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Я запускаю этот код вcodeandbox. Вопрос: Как решить эту проблему и есть ли пример кода, на который я могу ссылаться?. Спасибо
@Rahamin Я использую клиентскую библиотеку nodejs googlemaps. Невозможно добавить другой URL-адрес к этому URL-адресу, определенному в клиентской библиотеке nodejs goggle maps.
В итоге я использовал обходной путь cors -where.





Обходной путь: stackoverflow.com/questions/49001011/…