我想用 Google'sDinstanceMatrixService獲取多個點之間的距離,但出現以下錯誤:

我使用了google.maps...多次,它給了我關于google未定義的錯誤。我在互聯網上搜索,發現如果我window在前面輸入google.maps...它應該可以作業,但它沒有
function calculate_furthest_point() {
var origin1 = new window.google.maps.LatLng(46.754516, 23.596608);
var origin2 = new window.google.maps.LatLng(46.753906, 23.582223);
var destinationA = new window.google.maps.LatLng(46.751362, 23.594867);
var destinationB = new window.google.maps.LatLng(46.754986, 23.592378);
var service = new window.google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: "DRIVING",
transitOptions: TransitOptions,
drivingOptions: DrivingOptions,
unitSystem: UnitSystem,
avoidHighways: Boolean,
avoidTolls: Boolean,
},
callback
);
// callback()
function callback(response, status) {
if (status == "OK") {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i ) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j ) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
console.log(
"DISTANCE : " distance " FROM: " from " TO: " to
);
}
}
} else console.log("ERROR");
}
}
和我的進口:
import logo from "./logo.svg";
import "./App.css";
import {
GoogleMap,
useLoadScript,
Marker,
InfoWindow,
} from "@react-google-maps/api";
import { useEffect } from "react";
import { initializeApp } from "firebase/app";
import {
getDatabase,
ref,
set,
onValue,
get,
child,
database,
DataSnapshot,
} from "firebase/database";
我正在使用react-google-maps.
uj5u.com熱心網友回復:
如果您將模塊中的庫用作匯入,您將無法訪問window.google. 如果你calculate_furthest_point在模塊外呼叫你的函式,你應該在你的標簽中添加谷歌地圖腳本<head>:
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>
否則,您應該從@react-google-maps包中匯入和使用方法。
uj5u.com熱心網友回復:
Maibe 在使用 window.google 之前您需要加載事件。
來自檔案:如果您需要訪問地圖物件,而不是 ref prop,則需要在組件上使用 onl oad 回呼。
https://www.npmjs.com/package/@react-google-maps/api
uj5u.com熱心網友回復:
在某些時候這件事是undefind,所以我的建議是將 qm(?) 放在window.googlevar service = new window.google?.maps.DistanceMatrixService(); 之后
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/409198.html
標籤:
上一篇:從生成器中獲取生成器函式的名稱
