当我输入此地址'81 w Columbus Street,Lithopolis,OH 43136进入谷歌地图时,它确认地址,发回坐标,并在地图上显示标记。但是,当我在我的脚本中使用相同的地址时,我得到一个完全不同的JSON地址/响应。 如果需要,可以使用一些代码:
表格:
<form action="json-decode-script.php" name="my-form" id="my-form" method="post">
<input name="street_add" id="street_add" type="textbox" placeholder="street addres" class="address">
<input name="city" id="city" type="textbox" placeholder="city" class="address">
<input name="state" id="state" type="textbox" placeholder="state" class="address">
<input name="postcode" id="postcode" type="textbox" placeholder="zipcode" class="address">
<input type="submit" id="submit" value="go">
</form>
剧本:
<?php
$street_add = (isset($_POST['street_add'])) ? $_POST['street_add'] : '';
$city = (isset($_POST['city'])) ? $_POST['street_add'] : '';
$state = (isset($_POST['state'])) ? $_POST['street_add'] : '';
$postcode = (isset($_POST['postcode'])) ? $_POST['postcode'] : '';
$address = $street_add . ',' . $city . ',' . $state . ',' . $postcode;
$latitude = '';
$longitude = '';
// Send Encoded address to google api
$json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.rawurlencode($address).'&sensor=false');
// Convert json to array
$array = json_decode($json);
// Results
$formatted = $array->results[0]->formatted_address;
$latitude = $array->results[0]->geometry->location->lat;
$longitude = $array->results[0]->geometry->location->lng;
$theStreetNum = $array->results[0]->address_components[0]->long_name;
$theStreetName = $array->results[0]->address_components[1]->long_name;
$street_add = $theStreetNum . ' ' . $theStreetName;
$theCity = $array->results[0]->address_components[3]->long_name;
$theState = $array->results[0]->address_components[6]->short_name;
$thePostCode = $array->results[0]->address_components[8]->long_name;
?><pre><?php
print_r($array);
?></pre>
另请注意,我使用的其他地址按预期工作。