Hallo Ich konnte die JSON-Antwort von Google-Orten zurückholen und muss das Foto-Referenzelement daraus extrahieren.Iterieren über Google Orte JSON Antwort
Ich habe versucht, über die Orte Ergebnisse zu iterieren und für jede Stelle Ergebnisse die photo_reference, aber ich bekomme die Fehlermeldung.
TypeError: Kann Eigenschaft '0' oder undefined nicht lesen.
Was ich versucht habe:
response.on('end', function() {
var places = JSON.parse(body);
var locations = places.results;
locations.forEach(function(loc) {
var photoRef = loc.photos[0].photo_reference;
console.log('Photo ref ==' + photoRef);
});
});
ist hier eine Probe der JSON-Antwort.
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 46.7725141,
"lng" : 23.62686099999999
},
"viewport" : {
"northeast" : {
"lat" : 46.77284264999999,
"lng" : 23.62798785
},
"southwest" : {
"lat" : 46.77152845000001,
"lng" : 23.62348045
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
"id" : "18ce6ff7131f1ffd9ce9b2558ba407eed7409aee",
"name" : "Hotel Univers T",
"photos" : [
{
"height" : 1066,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116854836566418330262/photos\"\u003eHotel Univers T\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAE6Zi31LvtXmRM0orTBKDbJ3pgENKS82rVyP-462YhvrZAhhiDp2xowEiWsDNPU8tTmkXkhhB2ADKHi5GcptXSDx16GtG6RL9gpmogJw9BIR1JDiWSgMOzIE9xguxAQJ_s6Q9VHv77tQAmukc7JTJmBF_bFKQF3pVcmH6ffuUuiZEhDaF5BmQFqZRyeDOAA0KBkdGhSYpD-28mHr9XOKRb8dTheHJLA5bg",
"width" : 1069
}
],
"place_id" : "ChIJPzw_f2oMSUcRNl_Mz_sQS2c",
"rating" : 4,
"reference" : "CmRRAAAAGFISd-ET8V96LNII_v7yz5W80IuYIkLPyEPkf2dXWXudbM8YOfzaLDGMV1oNFl2YpN6dWEIDGl4TmAFP2wEYHPklveu-fXdFjUWA-w5cKapzxv0fdhSlSk4HkzpG9YN8EhDuT1KZUzs-O2h_eIjhgu5_GhSND6qUA9VG54QBT3csNWMaG_75Lw",
"scope" : "GOOGLE",
"types" : [
"gym",
"spa",
"lodging",
"health",
"restaurant",
"food",
"point_of_interest",
"establishment"
],
"vicinity" : "Strada Alexandru Vaida Voevod 53-55, Cluj-Napoca"
},
{
"geometry" : {
"location" : {
"lat" : 46.7720145,
"lng" : 23.6257946
},
"viewport" : {
"northeast" : {
"lat" : 46.77209325,
"lng" : 23.62638825
},
"southwest" : {
"lat" : 46.77177825,
"lng" : 23.62401364999999
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "61f80d1a74e00b55a075cacde6fc95aa88bfa83f",
"name" : "Pizza Hut Iulius Mall",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 2322,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/112054611800145890609/photos\"\u003etamara ionescu\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAAOdAY1sLwHqWZ1sEFvhZrqAoFCNo7DbthSnLMn1aI6lGxmGhfJdE7dLFDsJxjwSyh_0L3F2Mf_-Si12UmJ9CtaySu6SjHrqnaoF1WdafNDNLbkDbqjvesU0FqJid9V8poKU8Hhtb2PAvRe8lkfJF6N1ujIWWOxQlRfhHdjqN_BAAEhCTFmdXtQjfWiC3qbOZO3SqGhTIsansBf4AxGYnt6FnoslOVrqtyw",
"width" : 4128
}
],
"place_id" : "ChIJ-0XLwGoMSUcR87bDPcmCLHI",
"price_level" : 1,
"rating" : 4.1,
"reference" : "CmRRAAAAby6PokNes7fY7vSBVfxZvOfZj5wehpXYD8oUFAYRgmyOwreZ-hsxByGOCb0SRRfD73muHIScP0lK7fVlMk7g2-B1tOqxHt60oDKPkfgbe42QogLNVDnAmjo1nGrtHGzUEhB9TPDl0pw_CSCpmcQkERp8GhT_kUf3Xb1CQLliMvvc_BStebrt_w",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "Strada Alexandru Vaida Voevod 55, Cluj-Napoca"
},
{
"geometry" : {
"location" : {
"lat" : 46.7731014,
"lng" : 23.627801
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "32a93ffe70bbdd2c8485e346837ad61384e67bf1",
"name" : "Hanul Dacilor",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/102010465058487677046/photos\"\u003eחוסין מוחמד\u003c/a\u003e"
],
"photo_reference" : "CoQBdwAAALGMxPZEorGPgWzFhEX8GQ6KWqvl1Gs5s4w8n0-_wyFWNQ2AaxbUXjIZK_EXnEbI6UCM4YhVyjdObdpVyEtJ6oMJ1MG_GtW_BJi2Mc-75PPBX500rAbAy_jz-8L9NL4pdC1wHF6cwWj08xx2zBHammjvvB-DD_yeSjP5qt-GV09PEhDhOXdgy57rEKjg0N5pZ01CGhTeRa3PSqE-RkpI5vGjdDObho2AwQ",
"width" : 5312
}
],
"place_id" : "ChIJmRm5HmsMSUcRLIcHKiIRKmY",
"rating" : 4.4,
"reference" : "CmRRAAAA1mC_ed2g3YhFhzsrY8fXgE4Xida9jQ9BxpIk6raJpNmFUEJn00JciB3PRisZfOCpB3034vayac5_dqY8Bp_eQnS1HBSC0IdQp_Hu4Dcb_krbWQs6Sl7bTVSD5Rsu9LqmEhCDZdOMO2YJCNCh7n3-t0qbGhTI9ibzMsLx0OQUQ_oihQkUdr_Yhg",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "Iulius Mall, Strada Alexandru Vaida Voevod 53-55, Cluj-Napoca"
},
{
"geometry" : {
"location" : {
"lat" : 46.771945,
"lng" : 23.626012
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "8d7bb32fd50d968c79c2e5048c12b95cec8cbb7d",
"name" : "Marty City",
"opening_hours" : {
"open_now" : false,
"weekday_text" : []
},
"photos" : [
{
"height" : 1125,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/114164649734885665448/photos\"\u003eMarty City\u003c/a\u003e"
],
"photo_reference" : "CoQBcwAAAAfjwkLAWX73DgVUjKgP2uS39gFUilk6CrHCfdy-V4d1ZJrdZKzHOeqNqU0f5gHy9-MWeNGsY_2q7cJpi_b9_5oYneyQWs4U-zN6l5DxpeMdtrnNuNwd31b3BA_IEy7q_JZ8IlMScrqRspSBmq6tfFaTdnNZUwr0acvEOCVnwp2MEhCw0oErjn23GJoa5knp4gZuGhQ5ztJ1RbSe1zJbhCzAqJvAm-fN-Q",
"width" : 1500
}
],
"place_id" : "ChIJU22Cv2oMSUcR-rhmfyKJixA",
"rating" : 3.8,
"reference" : "CmRRAAAAHjaKSuIN_npID0s-cC2oJAJxwLZv-sOz0J9OW8AdiRzoqlQE1MMKH3aWDwKLB9P18uGxzgefxBnkc1_E4s-4gpJsTdmoATlxSoxsuQPGl4W0pXVHBZzpnvPHCSK5GyHEEhDarUaahHk1WNi7jzExovQ3GhS-KvRprEN1uPj-cw6PJcIqiDhl8A",
"scope" : "GOOGLE",
"types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
"vicinity" : "str. Alexandru Vaida Voievod 53, Cluj-Napoca"
},
Ich denke manchmal, gibt es kein Foto-Objekt in der Antwort (ohne Fotos für die Plätze vorhanden sind). Können Sie überprüfen, ob dies der Fall ist, indem Sie die gesamte Antwort auf die Konsole drucken und prüfen, ob sie für jeden Standort existiert? Ich habe es hier versucht - https://repl.it/EW5r/0 und es scheint gut zu funktionieren für das Beispiel, das Sie angegeben haben. –