使用json和php进行谷歌地理编码

时间:2011-01-14 10:24:09

标签: php json curl

我的php文件:

<?php

 if (isset($_POST['query'])) {  
    $query = $_POST['query'];

$url='http://maps.googleapis.com/maps/api/geocode/json?address=';
$callback="&callback=?";
$sensor='&sensor=false';
$result= $url.$query.$sensor.$callback;
curl_init ($result);

 }
?>

我的html文件:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){


    // when the user clicks the button
    $("button").click(function(){


          $.getJSON("geo.php",function(json){

             $('#results').append('<p>Latitude : ' + json.results[9].geometry.location.lat+ '</p>');
             $('#results').append('<p>Longitude: ' + json.results[9].geometry.location.lng+ '</p>');

});

          // get the json file
    });
});


</script>

</head>
<body>

<input type="text" id="query" /><button>Get Coordinates</button>
<div id="results"></div>

</body>
</html>

当我使用firbug时出现此错误:

json is null
$('#results').append('<p>...s[9].geometry.location.lat+ '</p>'); 

Google的json回复:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

编辑:

<?php
if (isset($_POST['query'])) {  
    $query = $_POST['query'];
$url='http://maps.googleapis.com/maps/api/geocode/json?address=';
$callback="&callback=?";
$sensor='&sensor=false';
$result= $url.$query.$sensor.$callback;
$resp = file_get_contents($result);
                header('Cache-Control: no-cache, must-revalidate');
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Content-type: application/json');
echo $resp;
}

2 个答案:

答案 0 :(得分:2)

您没有从PHP输出任何内容。 PHP脚本必须为getJSON输出JSON才能接收它。

看看curl_init。我想你想这样做:

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $result);
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

答案 1 :(得分:1)

您确定要捕获结果吗?

我假设这样: 您的PHP代码是通过HTML从Ajax调用的。 所以基本上你构建查询的URL并使用CURL来获取json结果。 看来你实际上并没有捕获结果或将结果返回给ajax调用 你可能想这样做:


            $process = curl_init($url); 
//init curl connection
            curl_setopt($process, CURLOPT_HEADER, 0); 

              curl_setopt($process, CURLOPT_POST, 1); 

            curl_setopt($process, CURLOPT_RETURNTRANSFER,1);

            curl_setopt($process,CURLOPT_CONNECTTIMEOUT,1);

            $resp = curl_exec($process); 
//your content
            curl_close($process); 
//$resp contains your response
                header('Cache-Control: no-cache, must-revalidate');
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Content-type: application/json');
echo $resp;