doufei1988
2018-09-27 10:59 阅读 55

在谷歌地图上显示多个api结果照片

Get 20 results of (example:School ) from google places api and i want to show photos on google map but some api results does not contain any image so how i can fix it. Error show: undefined index 'photos'.

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

Get the result of schools from google places api the index[1] does not contain any image than how to show on map.

                 [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
                    )

            )
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dtp0760 dtp0760 2018-09-27 11:27

    you should first check if photo key is exist in $resp['results'][$i] array by using isset function in php

    for example

    if(isset($resp['results'][$i]['photos']) && count($resp['results'][$i]['photos']) > 0)
    {
      // write your code
    }
    

    and also you should use phpenter code here count function to check $resp['results'][$i]['photos'] array has multi entry so you can write $resp['results'][$i]['photos'][0] without have out of scope error

    点赞 评论 复制链接分享

相关推荐