如何从 google 附近搜索 api 获取图像 url

How to get image url from google nearby search api

我正在尝试使用 google nearbyApi 查找附近的地点,它按预期工作。但是,问题是我无法从 json 响应中获取地点图像。请看下面的代码。

{
  "geometry": {
    "location": {
      "lat": 24.331058,
      "lng": 54.623759
    },
    "viewport": {
      "northeast": {
        "lat": 24.332406980291,
        "lng": 54.625107980292
      },
      "southwest": {
        "lat": 24.329709019708,
        "lng": 54.622410019708
      }
    }
  },
  "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
  "id": "6c79339941d125d001a2116834f3257346b150bd",
  "name": "Mafraq Hotel & Resort",
  "photos": [{
    "height": 4032,
    "html_attributions": [
      "<a href=\"https://maps.google.com/maps/contrib/100504071164796744561/photos\">ناصر الكعبي</a>"
    ],
    "photo_reference": "CoQBdwAAAF_hJoCjWHzDXpfEWbS-Ubg3cPB1Eac2y7fy6HM0q1sb4TyOZdci4LUQi7Z8TYXVw-ZbjeSDXkg-wwHjr67Ib2Aluv5QChRl3C97gTmlseV7UpUnvyW3Vt4GmcyQGRhfEkhjimbqjkUE8qC-8dt7thsRXzw7TIaBrEii1wK5C7gUEhCfuLSsZdEZkgoMEy0luSyeGhTfNMKnTkqmzUlUtTwdO7Gwx8tDYQ",
    "width": 3024
  }],
  "place_id": "ChIJbURih5o3Xj4R4qP2Jcu8C8k",
  "rating": 3.9,
  "reference": "CmRSAAAAMdmAyoaneUbzSmqHVbG_SFpgdcRjKSxQcHmVLKO518kINmhukPJF5Qo8tju2Su7uekMOXRw5jJxiNbBzjHY7Cd72FjnYEbx87N2F3OZoAUfUC9XuJ52uJmiJtXKRk9SkEhAg9HLkwuO58r4iVvgBqnY8GhTVfRwqPvEULqLIqmYlyA3U6h_1pg",
  "scope": "GOOGLE",
  "types": [
    "gym",
    "lodging",
    "health",
    "restaurant",
    "food",
    "point_of_interest",
    "establishment"
  ],
  "vicinity": "Sheikh Maktoum Bin Rashid Road, Al Mafraq"
}

我正在获取照片数组:

"photos": [{
  "height": 4032,
  "html_attributions": [
    "<a href=\"https://maps.google.com/maps/contrib/100504071164796744561/photos\">ناصر الكعبي</a>"
  ],
  "photo_reference": "CoQBdwAAAF_hJoCjWHzDXpfEWbS-Ubg3cPB1Eac2y7fy6HM0q1sb4TyOZdci4LUQi7Z8TYXVw-ZbjeSDXkg-wwHjr67Ib2Aluv5QChRl3C97gTmlseV7UpUnvyW3Vt4GmcyQGRhfEkhjimbqjkUE8qC-8dt7thsRXzw7TIaBrEii1wK5C7gUEhCfuLSsZdEZkgoMEy0luSyeGhTfNMKnTkqmzUlUtTwdO7Gwx8tDYQ",
  "width": 3024
}],

但是,我不知道如何根据这些数组信息创建图像。 (我正在使用网络 api,我需要图像 url)

如有任何帮助,我们将不胜感激。

提前致谢

使用 photo_reference

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=

获取图片url:

let data = {} // the json response

let url = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=";

let photoRef = data["photos"][0]["photo_reference"];

let imageUrl = url + photoRef;

Source

TL;DR

地点照片请求是一个 HTTP URL,格式如下:

https://maps.googleapis.com/maps/api/place/photo?
  • maxwidth=[您 json 中的宽度值(照片[0] -> 宽度)]
  • &
  • photoreference=[照片[0] -> photo_reference]
  • &
  • key=[你的 api 密钥]

所以你的最终 URL 将是

https://maps.googleapis.com/maps/api/place/photo?maxwidth=5184&photoreference=CmRaAAAADmroqcfJMAxlbbpIC4r8bk5jU-8fPx5udw2ng-XPvO9a-E46QlvggSxlgkxDsnjJyKacRSqvPkm69dbEvPlX6fgaGtEaApGROn-U6wDjpXlr5PTcjEDN6UACil904bHMEhAtOschikf4lATKyBULOsnLGhTQTXKMKJnV_Z_0kdqUIyh6ZIDZ0A&key=xyzyourapikey