Skip to content

Instantly share code, notes, and snippets.

@lunaluna
Last active August 29, 2015 13:57
Show Gist options
  • Save lunaluna/9701703 to your computer and use it in GitHub Desktop.
Save lunaluna/9701703 to your computer and use it in GitHub Desktop.
【WordPress】GoogleMapsAPIを利用して住所から緯度・経度を取得する【PHP】
<?php
$map_address = get_post_meta($post -> ID, 'googlemap_address', true);
$map_marker = get_post_meta($post -> ID, 'googlemap_label', true);
// エスケープ
$map_address = esc_html( $map_address );
// 全角英文字・数字など整形
$map_address = mb_convert_kana( $map_address, "nKV", "UTF-8" );
// urlエンコード
$map_address = urlencode( $map_address );
// google maps api を呼び出して変数に代入
$api_uri = 'http://maps.googleapis.com/maps/api/geocode/json?address='. $map_address .'&sensor=false';
// jsonファイルを取得
$json = file_get_contents($api_uri, true);
// jsonファイルから配列に展開
$array = json_decode( $json, true );
$code = $array[status];
if ( $code == 'OK' ) {
// jsonファイルから配列に展開
$latitude = $array[results][0][geometry][location][lat];
// jsonファイルから配列に展開
$longitude = $array[results][0][geometry][location][lng];
$coordinates = $latitude. ',' . $longitude;
} else {
$coordinates = false;
}
$map_magnification = get_post_meta($post -> ID, 'googlemap_magnification', true);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment