【问题标题】:How to get an image from google places api?如何从谷歌地方 api 获取图像?
【发布时间】:2020-12-28 20:24:10
【问题描述】:
【问题讨论】:
标签:
reactjs
google-maps
axios
xmlhttprequest
base64
【解决方案1】:
如果您想在客户端应用程序中显示 Places API 照片,您需要使用 Google Maps JavaScript Places Library 的 Places Photos。
下面的代码 sn-p 显示了我如何从 getDetails 结果中获取照片 URL 并将其放入我的状态变量中的示例:
const request = {
placeId: "ChIJN1t_tDeuEmsRUsoyG83frY4",
fields: [
"name",
"formatted_address",
"place_id",
"geometry",
"photos"
]
};
const service = new google.maps.places.PlacesService(map);
service.getDetails(request, (place, status) => {
if (
status === google.maps.places.PlacesServiceStatus.OK &&
place &&
place.geometry &&
place.geometry.location
) {
const marker = new google.maps.Marker({
map,
position: place.geometry.location
});
}
this.setState({
photos: place.photos[0].getUrl(),
name: place.formatted_address
});
});
这是sample code,要查看这项工作,请将Map.js 内脚本中的YOUR_API_KEY 替换为您的API 密钥。