【发布时间】:2021-11-10 21:20:52
【问题描述】:
我正在创建一个 Typescript 应用程序,它从用户那里获取地址并将其显示在 Google 地图上。
我创建了一个type GoogleGeocodingResponse,它接受地理编码response.data.results 作为{geometry: {location: {lat: Number; lng: Number}}}
然后我用 Axios 来:
.get<GoogleGeocodingResponse>(
`https://maps.googleapis.com/maps/api/geocode/json?
address=${encodeURI(enteredAddress)}&key=${GOOGLE_API_KEY}`
)
我不明白的部分是,如何使用这些数据创建LatLng?我一直在使用:
const coordinates = new google.maps.LatLng({
lat: Number(response.data.results[0].geometry.location.lat),
lng: Number(response.data.results[0].geometry.location.lng),
})
强制转换为Number 有效,但似乎我应该能够直接从GoogleGeocodingResponse 获取数据,而无需先进行强制转换。我必须专门定义一个类型吗? @types/google.maps 中是否有我可以使用的类型?还有什么?
【问题讨论】:
标签: javascript typescript google-maps typescript-types