如何从谷歌附近的搜索API获取图像网址

时间:2017-02-27 01:30:12

标签: json google-maps google-nearby

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

def form_valid(self, form):
    self.object = form.save(commit=False)
    self.object.user = self.request.user
    self.object.companyprofile = CompanyProfile.objects.get(user=self.request.user)
    self.object.save()
    return super(CompanyDataCreateView, self).form_valid(form)

我正在拍照片:

{
  "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"
}

但是,我不知道如何从这个数组信息中创建图像。 (我正在使用web api,我需要图片网址)

真的很感激任何帮助。

先谢谢

2 个答案:

答案 0 :(得分:1)

使用photo_reference

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

获取图片网址:

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;

答案 1 :(得分:0)

https://secure.php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-notes

<强> TL; DR

地方照片请求是以下格式的HTTP网址:

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

所以你的最终网址将是

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