从Google Places api获取20个结果(例如:School),我想在Google Map上显示照片,但一些api结果不包含任何图像,因此我可以解决它。错误显示:未定义索引“照片”。
if ($resp['status']=='OK') {
//Loop through get the multiple results
for ($i=0; $i < count($resp['results']) ; $i++) {
//Get the Important Data
$data['formatted_address'] = $resp['results'][$i]['formatted_address'];
$data['latitude'] = $resp['results'][$i]['geometry']['location']['lat'];
$data['longitude'] = $resp['results'][$i]['geometry']['location']
enter code here['lng'];
$data['icon'] = $resp['results'][$i]['icon'];
$data['name'] = $resp['results'][$i]['name'];
$data['height'] = $resp['results'][$i]['photos'][0]['height'];
$data['html_attributions'] = $resp['results'][$i]['photos'][0]
['html_attributions'][0];
$data['photo_reference'] = $resp['results'][$i]['photos'][0]
['photo_reference'];
$data['width'] = $resp['results'][$i]['photos'][0]['width'];
//Verify if Data Exist
if (!empty($data) && !empty ($data['latitude']) && !empty
($data['longitude'])) {
print_r($data);
}else{
echo "<h1 style=color:red;>Values Empty</h1>";
}
}//End for-loop
}else{
echo "<h1 style=color:red;>Status Error</h1>";
}
从Google Places api获取学校的结果,索引[1]除了在地图上显示方式外,没有其他图像。
[results] => Array(
[0] => Array
(
[formatted_address] => Tariqabad Multan, Punjab, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2073928
[lng] => 71.4530978
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.208742629893
[lng] => 71.454447629893
)
[southwest] => Array
(
[lat] => 30.206042970107
[lng] => 71.451747970107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => de1405f04fffe5f2910789717344e513625aada3
[name] => Learner's Castle School Multan
[photos] => Array
(
[0] => Array
(
[height] => 730
[html_attributions] => Array
(
[0] => Faisal Mushtaq
)
[photo_reference] =>
CmRaAAAAvhDiC7bMqpf3F5lvLSUmDkv8aAqSF1AaC_EoeUhG
f40GCU3H0zaLOQZ96fRZxTJ9goxxh2C
CfJXtJyXfMOBBqPPsOAKenERrefpNwtdV07nDyPE62nSdz3aO7T1aQaMy
EhBQOTSeTEKvNxBCk5rxl7 FUGhQ9MSlemG_o0jxeYE9f0_RmtnQzrQ
[width] => 1296
)
)
[place_id] => ChIJqfKsD8IzOzkRomMlLG80aho
[plus_code] => Array
(
[compound_code] => 6F43+X6 Multan, Pakistan
[global_code] => 8J2H6F43+X6
)
[rating] => 5
[reference] => ChIJqfKsD8IzOzkRomMlLG80aho
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
[1] => Array
(
[formatted_address] => N Gulgasht Blvd, Basti Mehmood Kot,
Multan, Punjab 60000, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2464553
[lng] => 71.483087
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.247805129893
[lng] => 71.484436829893
)
[southwest] => Array
(
[lat] => 30.245105470107
[lng] => 71.481737170107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => 8648cd48c320f11b001a0eece814bd72abc07231
[name] => Zakariya Public School
[opening_hours] => Array
(
[open_now] => 1
)
[place_id] => ChIJ5c1N-YM0OzkRKKtxXEhilow
[plus_code] => Array
(
[compound_code] => 6FWM+H6 Multan, Pakistan
[global_code] => 8J2H6FWM+H6
)
[rating] => 4.4
[reference] => ChIJ5c1N-YM0OzkRKKtxXEhilow
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
[2] => Array
(
[formatted_address] => 17- Officers Colony, Officers Colony,
Multan, Punjab, Pakistan
[geometry] => Array
(
[location] => Array
(
[lat] => 30.2105142
[lng] => 71.4726681
)
[viewport] => Array
(
[northeast] => Array
(
[lat] => 30.211864029893
[lng] => 71.474017929893
)
[southwest] => Array
(
[lat] => 30.209164370107
[lng] => 71.471318270107
)
)
)
[icon] =>
https://maps.gstatic.com/mapfiles/place_api/icons/school-71.png
[id] => 8c809b4c5a4b8355899ddf6b6a7497bb8cb43be1
[name] => Jinnah Highs School System
[opening_hours] => Array
(
[open_now] => 1
)
[photos] => Array
(
[0] => Array
(
[height] => 1920
[html_attributions] => Array
(
[0] => A Google User
)
[photo_reference] =>
CmRZAAAAmQ8VIRfu9_s5UTrmBRwHpPWYuPjXWWcRj5KnWGwTy4yXo-
h2bWa8NSXrYSI8ukYD8nhfQRVxjxVKfWMxJlC4Xn8D9KBmoB7M9P
CZU5_XHvZ2heWSXPkqcGvjlcDiMalsEhA3VzcWjoDnL9Fs7Bs8PP
t4GhSePGS5CmCg0AXA8kEfl91iWotZgA
[width] => 2560
)
)
[place_id] => ChIJz4eKo_YzOzkRHvCbt43xkq8
[plus_code] => Array
(
[compound_code] => 6F6F+63 Multan, Pakistan
[global_code] => 8J2H6F6F+63
)
[rating] => 4.3
[reference] => ChIJz4eKo_YzOzkRHvCbt43xkq8
[types] => Array
(
[0] => school
[1] => point_of_interest
[2] => establishment
)
)
答案 0 :(得分:0)
您应该首先使用php中的isset函数检查$ resp ['results'] [$ i]数组中是否存在照片键
例如
if(isset($resp['results'][$i]['photos']) && count($resp['results'][$i]['photos']) > 0)
{
// write your code
}
,并且您还应该使用php enter code here
计数功能来检查$ resp ['results'] [$ i] ['photos']数组是否具有多个条目
因此您可以编写$ resp ['results'] [$ i] ['photos'] [0]而不会出现超出范围的错误