如何从Google Places的JSON响应中获取参数

时间:2018-11-17 05:48:54

标签: react-native

问题:

我正在创建一个React应用程序。在这里,我使用Google Places API来获取附近的地方。为此,我创建了这种代码段。

fetch(
      "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" +
        coords.latitude +
        "," +
        coords.longitude +
        "&radius=500" +
        "&type=parking" +
        "&key=" +
        apikey
    )
      .then(response => response.json())
      .then(responseJson => {
        console.log(responseJson.results);
      });
    // this.setState({ location: location });
  };

对于此呼叫,我得到了这样的JSON响应。

[10:26:09]   Object {
[10:26:09]     "geometry": Object {
[10:26:09]       "location": Object {
[10:26:09]         "lat": 6.8471459,
[10:26:09]         "lng": 79.95188379999999,
[10:26:09]       },
[10:26:09]       "viewport": Object {
[10:26:09]         "northeast": Object {
[10:26:09]           "lat": 6.848494880291502,
[10:26:09]           "lng": 79.95323278029149,
[10:26:09]         },
[10:26:09]         "southwest": Object {
[10:26:09]           "lat": 6.845796919708499,
[10:26:09]           "lng": 79.95053481970848,
[10:26:09]         },
[10:26:09]       },
[10:26:09]     },
[10:26:09]     "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
[10:26:09]     "id": "e9ec7b7cc1084804271da28697e0342cb86f7c37",
[10:26:09]     "name": "PNH Carpark",
[10:26:09]     "opening_hours": Object {
[10:26:09]       "open_now": true,
[10:26:09]     },
[10:26:09]     "photos": Array [
[10:26:09]       Object {
[10:26:09]         "height": 3120,
[10:26:09]         "html_attributions": Array [
[10:26:09]           "<a href=\"https://maps.google.com/maps/contrib/115048704313755953511/photos\">Nimal Kumara</a>",
[10:26:09]         ],
[10:26:09]         "photo_reference": "CmRaAAAAYHzPo3aNai9pQkuhH7dlZIItras7XTW5buvyuqzDiKK6f4WFaOxrLpvtkW2gGcP6j9fTfDaJ2_n2XOOVIUOWuHQ1jDVjtSBahbsIgErIAKhoWyLy2teG4NNQYBN1iwH8EhD49eTzfJVhCMB1WOu2w6DLGhR8tV0F4uVSgMD8pGDn5mU1ImzzqQ",
[10:26:09]         "width": 4160,
[10:26:09]       },
[10:26:09]     ],
[10:26:09]     "place_id": "ChIJQV-fTkVQ4joRRkG7ZUTe7_Q",
[10:26:09]     "plus_code": Object {
[10:26:09]       "compound_code": "RXW2+VQ Pannipitiya, Sri Lanka",
[10:26:09]       "global_code": "6JRXRXW2+VQ",
[10:26:09]     },
[10:26:09]     "rating": 3,
[10:26:09]     "reference": "ChIJQV-fTkVQ4joRRkG7ZUTe7_Q",
[10:26:09]     "scope": "GOOGLE",
[10:26:09]     "types": Array [
[10:26:09]       "parking",
[10:26:09]       "point_of_interest",
[10:26:09]       "establishment",
[10:26:09]     ],
[10:26:09]     "vicinity": "Kottawa Town, Pannipitiya",
[10:26:09]   },
[10:26:09] ]

有人可以帮助我修改代码,以便从此JSON响应中获取纬度,经度和地名吗?我尝试了很多方法来完成该操作,但我无法做到这一点。非常感谢!!!

1 个答案:

答案 0 :(得分:1)

我假设行console.log(responseJson.results)为您提供了上面提供的JSON响应。如果是这样,则results是一个JSON对象,并且在其中有几个键值对。要访问该值,只需使用其键,如下所示:

.then(responseJson => {
    let name = responseJson.results.name;
    let latitude = responseJson.results.geometry.location.lat;
    let longitude = responseJson.results.geometry.location.lng;
  });