2
1

More than 3 years have passed since last update.

apple mapkitをNuxt.jsで表示する

Last updated at Posted at 2020-01-31

Nuxt.jsでappleのmapを表示するのに少しハマったので、サンプルです。

map.vue
<template>
  <!-- 地図用のコンテナを作成する -->

  <section class="container">
    <div id="map" /></div>
  </section>
</template>

<script>
export default {

  data: function(){
        return {
        };
  },

  async created () {
    await this.mount() 
    console.log(window.mapkit)


    // MapKit JSを初期化する
    window.mapkit.init({
      authorizationCallback: function(done) {
        done("-- API Key --");
        }
    });

    // Mapオブジェクトを作成する
    var map = new window.mapkit.Map("map");

    // 地図の表示範囲を指定する
    var tokyo = new window.mapkit.CoordinateRegion(
      new window.mapkit.Coordinate(35.6811673, 139.7670516),
      new window.mapkit.CoordinateSpan(0.04, 0.04)
    );
    map.region = tokyo;

  },
  methods: {
    async mount (version = '5.0.x') {
      return new Promise((resolve, reject) => {
        const src = `https://cdn.apple-mapkit.com/mk/${version}/mapkit.js`

        if (window.mapkit) {
          return resolve(window.mapkit)
        }

        const script = document.createElement('script')
        script.onload = () => resolve(window.mapkit)
        script.onerror = (error) => reject(error)
        document.body.appendChild(script)
        script.src = src
      })
    },
  }
}

</script>
<style>
        #map {
            width: 100%;
            height: 300px;
        }
</style>
2
1
1

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
2
1