投稿日: | 最終更新日:
カスタムフィールドでgooglemapを表示させる方法
ページにgooglemapを表示させます。
個人店や企業サイトのアクセスページによくある項目です。
一度作れば何度も使いまわしができて非常に便利です。
ソースコード:一番楽な方法
- ・個別ページ(single.php)にgooglemapのコードを記述します。
- ・住所の情報は、カスタムフィールドから取得します。
- ・カスタムフィールドの名前を仮に「カスタムフィールド名」とします。
- ・カスタムフィールド名のデータは、○○県○○市…などの文字列データです。
<?php if ( post_custom('カスタムフィールド名') ) : ?> <?php $addressformap = post_custom('カスタムフィールド名'); $addressformapencode = urlencode(mb_convert_encoding($addressformap,"UTF-8","auto")); ?> <iframe width="500px" height="500px" frameborder="5" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.jp/maps?q=<?php echo $addressformapencode; ?>&z=18&output=embed"> </iframe> <?php else : ?> <p>地図を表示できません。</p> <?php endif; ?>
解説
カスタムフィールドの有無を値判定
<?php if ( post_custom('住所') ) : ?>
カスタムフィールド「住所」の値が存在するかチェックします。
住所取得
$addressformap = post_custom('住所');
カスタムフィールド「住所」の値を変数に代入します。
iframeタグ
<iframe width="500px" height="500px"・・・
ウィンドウの中に、独立したインラインのフレームを表示するためのタグです。
これを利用してgooglemapを表示します。
src="http://maps.google.co.jp/maps?q=<?php echo $addressformapencode; ?>&z=18&output=embed"
srcにgooglemapのURL+「住所」を記述します。
else以下の処理
<p>地図を表示できません。</p>
カスタムフィールドが存在しなかった場合の処理を記述します。
カスタムフィールド「住所」が無かった場合、「地図を表示できません。」を表示します。
ソースコード:個別ページからテンプレートを呼び出す方法
googlemapの吹き出しをもっと詳しく表示したい場合があります。
そんなときは、以下のようにJavaScriptを応用して設定します。
- ・個別ページ(single.php)からグーグルマップのページ(maps-template.php)を呼び出します。
- ・住所の情報は、カスタムフィールドから取得します。
- ・カスタムフィールド名を「住所」とします。
<?php $gmap = get_post_meta($post->ID, '住所', true); if ($gmap) { ?> <script type=”text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> var g = new google.maps.Geocoder(), map, center ; g.geocode({ 'address': '<?php echo $gmap; ?>' }, function(results, status) { if (status ==google.maps.GeocoderStatus.OK) { center = results[0].geometry.location; initialize(); } }); function initialize() { var options = { center: center, zoom: 14, mapTypeId: google.maps.MapTypeId.ROADMAP, scrollwheel: true, panControlOptions: { position: google.maps.ControlPosition.TOP_RIGHT }, zoomControlOptions: { position: google.maps.ControlPosition.TOP_RIGHT }, mapTypeControlOptions: { position: google.maps.ControlPosition.TOP_CENTER }, }; map = new google.maps.Map(document.getElementById('googlemap'), options); //マーカー作成 var marker = new google.maps.Marker({ position: map.getCenter(), //マーカーの位置 map: map //表示する地図 }); //吹き出しを作成します var contentString = '<?php echo "<strong>○×商事</strong> <br/>" ; ?>' + '<?php echo post_custom('map_adress'); ?>'; var infowindow = new google.maps.InfoWindow({ content: contentString // 吹き出し内コメント }); //吹き出しをオープンします infowindow.open(map,marker); //クリックしたときに吹き出しがオープンするイベントを定義 google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); } </script> <?php } ?> <?php if(post_custom('住所')): ?> <div id="googlemap"></div> <?php else: ?> <div id="no_map_div">地図はありません。</div> <?php endif; ?>
解説
情報ウィンドウ内に表示するテキスト
var contentString =
地図の情報ウィンドウ(吹き出し)に表示するテキストを指定します。HTML文も記述できます。
吹き出しをオープン
infowindow.open(map,marker);
地図の情報ウィンドウ(吹き出し)をオープンにします。
吹き出しを非表示にしたい場合はinfowindow.close();を記述します。
地図の表示
<div id="googlemap"></div>
「id=”googlemap”」のdivタグで地図を表示します。
- Python 114
- 制作 54
- RaspberryPi 41
- Django 40
- WordPress 40
- Linux 27
- VPS 22
- JavaScript 21
- PHP 20
- HTML・CSS 19
- AWS 16
- 仮想環境 15
- レスポンシブデザイン 13
- マイコン 11
- WEB全般 11
- 動画製作 9
- Webサービス 8
- 統合開発環境 8
- 機械学習 8
- PyCharm 7
- jQuery 7
- AfterEffects 7
- 起業・設立 7
- Django REST framework 6
- C# 6
- デザイン 6
- SEO 6
- pydata 6
- Visual Studio 5
- 数学 5
- 携帯サイト 5
- heroku 5
- Mac 5
- illustrator 5
- node.js 5
- Anaconda 5
- Nginx 4
- Jupyter Notebook 4
- インフラ 4
- Google Colaboratory 4
- symfony 4
- Webスクレイピング 3
- photoshop 3
- Go言語 3
- PC 3
- ツール 3
- Docker 3
- facebook 3
- 作業効率化 3
- データベース 3
- Cloud9 3
- コマンド 2
- micro:bit 2
- Kali Linux 2
- Webサーバー 2
- MariaDB 2
- ドローン 2
- コンテナ 2
- DaVinci Resolve 2
- ネットワーク 2
- Java 2
- movie 2
- PCDJ 2
- 音楽 2
- XSERVER 2
- Ansible 1
- Vue.js 1
- JSON 1
- Bootstrap 1
- バージョン管理システム 1
- SSL 1
- S3 1
- ムームードメイン 1
- ネットワーク 1
- アニメーション 1
- D3.js 1
- Rhino 1
- アニメ 1
- git 1
- windows 1
- アクセス解析 1
- スマートフォン 1
- アフィリエイトノウハウ 1
- 知識 1
- TypeScript 1
- 役立つ本・書籍 1
- データサイエンス 1
- ESP32 1
- AI 1
- ownCloud 1
- API 1