【问题标题】:Create a Google Map Using Zend Gdata使用 Zend Gdata 创建 Google 地图
【发布时间】:2012-02-21 23:01:43
【问题描述】:

我已经知道如何使用 Zend Gdata 对地址进行地理编码,但我什至不知道如何检索返回的 JSON 片段。这是地理编码器:

include_once('Zend/Gdata.php');
$client = new Zend_Http_Client('http://maps.googleapis.com/maps/api/geocode/json');

$urlencodedAddress = urlencode('1600 Pennsylvania Avenue, N.W. Washington, DC 20500');

$client->setParameterGet('sensor', 'false');
$client->setParameterGet('address', $urlencodedAddress); 
$response = $client->request('GET');    

这是响应对象:

Zend_Http_Response Object ( [version:protected] => 1.1 [code:protected] => 200 [message:protected] => OK [headers:protected] => Array ( [Content-type] => application/json; charset=UTF-8 [Date] => Mon, 30 Jan 2012 03:26:25 GMT [Expires] => Tue, 31 Jan 2012 03:26:25 GMT [Cache-control] => public, max-age=86400 [Vary] => Accept-Language [Server] => mafe [X-xss-protection] => 1; mode=block [X-frame-options] => SAMEORIGIN [Connection] => close ) [body:protected] => { "results" : [ { "address_components" : [ { "long_name" : "1600", "short_name" : "1600", "types" : [ "street_number" ] }, { "long_name" : "Pennsylvania Ave NW", "short_name" : "Pennsylvania Ave NW", "types" : [ "route" ] }, { "long_name" : "Northwest Washington", "short_name" : "Northwest Washington", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Washington", "short_name" : "Washington", "types" : [ "locality", "political" ] }, { "long_name" : "District of Columbia", "short_name" : "DC", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "20500", "short_name" : "20500", "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Pennsylvania Ave NW, Washington, DC 20500, USA", "geometry" : { "location" : { "lat" : 38.89871490, "lng" : -77.03765550 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 38.90006388029150, "lng" : -77.03630651970849 }, "southwest" : { "lat" : 38.89736591970851, "lng" : -77.03900448029150 } } }, "partial_match" : true, "types" : [ "street_address" ] } ], "status" : "OK" } )

我真的很想使用这些数据来创建一个拆分地图,一个带有标记的普通地图和一个显示街景的拆分视图。有什么建议?

【问题讨论】:

    标签: php zend-framework google-maps gdata


    【解决方案1】:

    真的,答案是忘记 Zend。真是浪费时间。做起来简单多了:

    $urlencodedAddress = urlencode('1600 Pennsylvania Avenue, N.W. Washington, DC 20500');
    
    $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $urlencodedAddress . "&sensor=false";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $details_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $geoloc = json_decode(curl_exec($ch), true);
    
    
    var_dump($geoloc['results'][0]['geometry']['location']['lat']);
    var_dump($geoloc['results'][0]['geometry']['location']['lng']);
    

    cUrl 是我最好的朋友。

    【讨论】:

      【解决方案2】:

      你可以这样做

      // get json object 
      json_decode($response->getBody());
      

      参考资料。 http://framework.zend.com/manual/en/zend.http.response.html

      【讨论】:

      • 感谢您,现在我如何从结果数据中获取位置:经纬度。它的显示如下: stdClass Object ( [lat] => 38.900063880291 [lng] => -77.036306519708 ) [southwest] => stdClass Object ( [lat] => 38.897365919709 [lng] => -77.039004480291 ) ) ) [partial_match] => 1 [类型] => 数组 ( [0] => street_address ) ) [status] => OK )
      • 你有数据现在添加到地图,这里是谷歌地图 javascript api 教程Google Maps JavaScript API V3
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多