use Google geocode to return street name and list of house numbers using php

不想你离开。 提交于 2020-01-06 02:41:11

问题


I have a registration form that I would like to fill with data when a user arrives on this section of the form. The first part of the form asks for the postcode/zip code. On the form load I would like to be able to display a list of house numbers with the street name.

This is what I have to return the data

<?php
$postcode = 'DH90ER';
$geourl = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$postcode&sensor=true");

$array = json_decode($geourl,TRUE);

print header("Content-type: text/plain");

print_r($array);

?>

This works it reruns an array of information that i need except the street name or numbers. For example if I echo

echo $array['results'][0]['address_components'][1]['long_name'];

This would echo the Town name. Its messy I know but it works really well. Is there a way I can get a the street name as postal codes are unique to street names and the number of possible houses?

I've been stuck for a while on this, I will use javascript if it is possible with javascript.

UPDATE to return single house number and street name

$geourl = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address=$postcode&sensor=true");

$array = json_decode($geourl,TRUE);

print header("Content-type: text/plain");


$lat = $array['results'][0]['geometry']['location']['lat'];

$lng = $array['results'][0]['geometry']['location']['lng'];


$add = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng");

$add = json_decode($add);
print_r($add);

来源:https://stackoverflow.com/questions/27228768/use-google-geocode-to-return-street-name-and-list-of-house-numbers-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!