通过php在地图googleapis com中的邮政编码错误

时间:2019-06-21 16:53:22

标签: google-geocoding-api

当我键入此网址https://maps.googleapis.com/maps/api/geocode/json?address=CARRETERA+B500%2C+km+3%2C+TIANA&key= *****

在我的网络浏览器中,我得到了正确的邮政编码:

{
    "results": [{
        "address_components": [{
            "long_name": "Barcelona",
            "short_name": "Barcelona",
            "types": ["administrative_area_level_2", "political"]
        }, {
            "long_name": "España",
            "short_name": "ES",
            "types": ["country", "political"]
        }, {
            "long_name": "08391",
            "short_name": "08391",
            "types": ["postal_code"]
        }],
        "formatted_address": "Carretera B-500 Badalona-Mollet km.3,2, 08391, Barcelona, España",
        "geometry": {
            "location": {
                "lat": 41.478713,
                "lng": 2.257954
            },
            "location_type": "GEOMETRIC_CENTER",
            "viewport": {
                "northeast": {
                    "lat": 41.4800619802915,
                    "lng": 2.259302980291502
                },
                "southwest": {
                    "lat": 41.4773640197085,
                    "lng": 2.256605019708498
                }
            }
        },
        "place_id": "ChIJV6E_B7a7pBIROWN9PW2WDc8",
        "plus_code": {
            "compound_code": "F7H5+F5 Mas Ram, España",
            "global_code": "8FH4F7H5+F5"
        },
        "types": ["establishment", "point_of_interest"]
    }],
    "status": "OK"
}

但是来自PHP的相同URL不会返回邮政编码:https://maps.googleapis.com/maps/api/geocode/json?address=CARRETERA+B500%2C+km+3%2C+TIANA&key= ****

array(2) { 
    ["results"]=> array(1) { 
        [0]=> array(6) { 
            ["address_components"]=> array(4) { 
                [0]=> array(3) { 
                    ["long_name"]=> string(5) "B-500" 
                    ["short_name"]=> string(5) "B-500" 
                    ["types"]=> array(1) { 
                        [0]=> string(5) "route" 
                    } 
                } 
                [1]=> array(3) { 
                    ["long_name"]=> string(9) "Barcelona" 
                    ["short_name"]=> string(9) "Barcelona" 
                    ["types"]=> array(2) { 
                        [0]=> string(27) "administrative_area_level_2" 
                        [1]=> string(9) "political" 
                    } 
                } 
                [2]=> array(3) { 
                    ["long_name"]=> string(9) "Cataluña" 
                    ["short_name"]=> string(2) "CT" 
                    ["types"]=> array(2) { 
                        [0]=> string(27) "administrative_area_level_1" 
                        [1]=> string(9) "political" 
                    } 
                } 
                [3]=> array(3) { 
                    ["long_name"]=> string(5) "Spain" 
                    ["short_name"]=> string(2) "ES" 
                    ["types"]=> array(2) { 
                        [0]=> string(7) "country" 
                        [1]=> string(9) "political" 
                    } 
                } 
            } 
            ["formatted_address"]=> string(23) "B-500, Barcelona, Spain"
            ["geometry"]=> array(4) { 
                ["bounds"]=> array(2) { 
                    ["northeast"]=> array(2) { 
                        ["lat"]=> float(41.5351073) 
                        ["lng"]=> float(2.2630393) 
                    } 
                    ["southwest"]=> array(2) { 
                        ["lat"]=> float(41.4492844) 
                        ["lng"]=> float(2.2089109) 
                    } 
                } 
                ["location"]=> array(2) { ["lat"]=> float(41.5020788) ["lng"]=> float(2.244613) } ["location_type"]=> string(16) "GEOMETRIC_CENTER" ["viewport"]=> array(2) { ["northeast"]=> array(2) { ["lat"]=> float(41.5351073) ["lng"]=> float(2.2630393) } ["southwest"]=> array(2) { ["lat"]=> float(41.4492844) ["lng"]=> float(2.2089109) } } } ["partial_match"]=> bool(true) ["place_id"]=> string(27) "ChIJ6QO8C9K-pBIRxf0esCJr374" ["types"]=> array(1) { [0]=> string(5) "route" } } } ["status"]=> string(2) "OK" }

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您需要一个简单的返回结果的json_decode()。

$results = json_decode($results, true);