Help us understand the problem. What is going on with this article?

住所または郵便番号から緯度経度を取得する関数

More than 5 years have passed since last update.

GoogleMapsAPIに住所を渡すと各種情報をXML形式で返してくれるのを利用して、住所から緯度経度を配列で返す関数を作成

function get_gps_from_address( $address='' ){
    $res = array();
    $req = 'http://maps.google.com/maps/api/geocode/xml';
    $req .= '?address='.urlencode($address);
    $req .= '&sensor=false';    
    $xml = simplexml_load_file($req) or die('XML parsing error');
    if ($xml->status == 'OK') {
        $location = $xml->result->geometry->location;
        $res['lat'] = (string)$location->lat[0];
        $res['lng'] = (string)$location->lng[0];
    }
    return $res;
}
$latlng = get_gps_from_address('東京都');
print_r($latlng);

//実行結果
/*
Array
(
    [lat] => 35.6894875
    [lng] => 139.6917064
)
*/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away