Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <button type="button" id="get-gps">位置情報取得</button>
<p id="lat">緯度: </p>
<p id="lon">経度: </p>
<p id="address"></p>

              
            
!

CSS

              
                
              
            
!

JS

              
                /** 変換表を入れる場所 */
var GSI = {};

const latEle = document.querySelector('#lat');
const lonEle = document.querySelector('#lon');
const addressEle = document.querySelector('#address');
const gpsButton = document.querySelector('#get-gps');
/**
   * 緯度経度を画面表示
   */
const setGeoLoc = (coords) => {
  latEle.textContent = `緯度: ${coords.latitude}`;
  lonEle.textContent = `経度: ${coords.longitude}`;
}
/**
   * 緯度経度から住所を取得して表示
   */
const getAddress = async (coords) => {
  // 逆ジオコーディング API
  const url = new URL('https://mreversegeocoder.gsi.go.jp/reverse-geocoder/LonLatToAddress');
  url.searchParams.set('lat', coords.latitude);
  url.searchParams.set('lon', coords.longitude);
  const res = await fetch(url.toString());
  const json = await res.json();
  const data = json.results;

  // 変換表から都道府県などを取得
  const muniData = GSI.MUNI_ARRAY[json.results.muniCd];
  // 都道府県コード,都道府県名,市区町村コード,市区町村名 に分割
  const [prefCode, pref, muniCode, city] = muniData.split(',');

  // 画面に反映
  address.textContent = `${pref} ${city} ${data.lv01Nm}`;
};
/**
   * 位置情報 API の実行(イベントリスナ)
   */
gpsButton.addEventListener('click', () => {
  navigator.geolocation.getCurrentPosition(
    geoLoc => {
      setGeoLoc(geoLoc.coords);
      getAddress(geoLoc.coords);
    },
    err => console.error({err}),
  );
});

// NOTE: 後から読み込みたいので CodePen 上では JS で処理
const script = document.createElement('script');
script.src = 'https://maps.gsi.go.jp/js/muni.js';
document.body.insertAdjacentElement('afterEnd', script);
              
            
!
999px

Console