【问题标题】:How do I access the protected object returned by geocoder-php/GeocoderLaravel?如何访问 geocoder-php/GeocoderLaravel 返回的受保护对象?
【发布时间】:2016-05-05 22:07:05
【问题描述】:

我安装了geocoder-php/GeocoderLaravel 包并根据文档进行了配置。然后我在控制器函数中运行了一个小测试,它发送到谷歌,解析地址并成功返回地理编码对象,但它受到保护,我无法访问它。这是怎么回事?

代码:

$geocode = Geocoder::geocode('9900 Sowder Village Square, Manassas, VA 20109');
var_dump($geocode);

输出:

object(Geocoder\Result\Geocoded)#189 (15) { 
    ["latitude":protected]=> float(38.7392838) 
    ["longitude":protected]=> float(-77.5348982) 
    ["bounds":protected]=> array(4) { 
        ["south"]=> float(38.7392838) 
        ["west"]=> float(-77.5348982) 
        ["north"]=> float(38.7392838) 
        ["east"]=> float(-77.5348982) 
    } 
    ["streetNumber":protected]=> string(4) "9900" 
    ["streetName":protected]=> string(21) "Sowder Village Square"     
    ["cityDistrict":protected]=> NULL 
    ["city":protected]=> string(8) "Manassas" 
    ["zipcode":protected]=> string(5) "20109" 
    ["county":protected]=> string(21) "Prince William County"     
    ["countyCode":protected]=> string(21) "PRINCE WILLIAM COUNTY" 
    ["region":protected]=> string(8) "Virginia" 
    ["regionCode":protected]=> string(2) "VA" 
    ["country":protected]=> string(13) "United States" 
    ["countryCode":protected]=> string(2) "US" 
    ["timezone":protected]=> NULL 
}

我在互联网上搜索并找到了有关反射的资料,但我认为它不可能那么复杂。这家伙在 gitHub 上的文档很烂。有什么指导吗?

感谢您抽出宝贵时间。

乔什

【问题讨论】:

    标签: php laravel oop geocode


    【解决方案1】:

    属性受到保护,因为 API 不希望您直接接触属性;他们想使用 getter 和 setter。

    http://geocoder-php.org/Geocoder/

    getCoordinates() 将返回一个坐标对象(具有纬度和经度属性);

    这就是你想要的。

    【讨论】:

    • 酷,谢谢。我以前看到过,但试图错误地调用它。它的:$geocode = Geocoder::geocode('9900 Sowder Village Square, Manassas, VA 20109')->getCoordinates();呃。非常感谢。
    猜你喜欢
    • 2014-10-15
    • 2013-01-22
    • 2017-11-24
    • 2013-10-02
    • 2015-06-12
    • 2013-01-05
    • 1970-01-01
    • 2013-07-19
    • 2019-10-06
    相关资源
    最近更新 更多