LoginSignup
47

More than 5 years have passed since last update.

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

Posted at

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
)
*/

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47