LoginSignup
3
0

More than 3 years have passed since last update.

【図解】Ionic Google Mapを表示するまでの全ての手順

Last updated at Posted at 2020-05-19

はじめに

基本的には普通にブラウザに表示させるのと、手順は同じ。

色々と調べたが、IonicでネイティブのGoogle Mapは使えない?(かもしれない)
→ @ionic-native/google-maps + cordova-plugin-googlemaps
これで使えるようです!

Google Cloud Platform Consoleはよく画面の見た目(UI)が変わるので、画像とボタンの配置が違ったりする事はご了承を。。。

環境

Ionic5 + Angular9 + capacitor

FirebaseまたはGoogle Cloud Platformでプロジェクトを作成

公式ページであるGet an API KeyではGoogle Cloud Platformへのリンクが貼られてるが、Firebaseで作ってもどっちでもいい。

今回はFirebaseでプロジェクトを作成したとする。

スクリーンショット 2020-05-19 22.15.04.jpg

APIキーを用意

Firebaseでプロジェクトを作った場合、すでに「Browser key (auto created by Firebase)」の名前でAPIキーが存在する。
スクリーンショット 2020-05-19 22.16.44.jpg

しかし、これとは別に新たに作成しよう。

Maps JavaScript APIを有効にする

Google Cloud Platform Consoleへ行く。

スクリーンショット 2020-05-19 22.30.50.jpg

Firebaseで作成したプロジェクトを、上のタブから選ぶ

スクリーンショット 2020-05-19 22.28.02.jpg

APIとサービス>ライブラリへ移動

スクリーンショット 2020-05-19 22.24.39.jpg

Maps JavaScript APIを選択

スクリーンショット 2020-05-19 22.07.32.jpg

有効にする

認証情報を作成

スクリーンショット 2020-05-19 22.33.38.jpg

APIとサービス>認証情報へ移動

スクリーンショット 2020-05-19 22.21.55.jpg

上にある「+認証情報を作成」ボタンを押し、APIキーを選択

スクリーンショット 2020-05-19 22.37.34.jpg

APIキーをコピーしておく

スクリーンショット 2020-05-19 22.36.02.jpg

APIの制限で、Maps JavaScript APIを選択し保存

Ionicプロジェクト内のindex.htmlにAPIキーが入ったコードを追加

公式ページGet an API Keyの中にあるscriptタグをコピー

スクリーンショット 2020-05-19 22.42.55.jpg

Ionicプロジェクト内のindex.html(結構上の階層にあるよ!)を開き、headの閉じタグの直前にscriptタグを追加

callback=initMapの部分は特にいらないので削除!

index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Ionic App</title>

  <base href="/" />

  <meta name="color-scheme" content="light dark" />
  <meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="msapplication-tap-highlight" content="no" />

  <link rel="icon" type="image/png" href="assets/icon/favicon.png" />

  <!-- add to homescreen for ios -->
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />

  <script async defer src="https://maps.googleapis.com/maps/api/js?key=Your_APIkey">
  </script>
</head>

<body>
  <app-root></app-root>
</body>

</html>

Mapを追加

home.page.html
<ion-header [translucent]="true">
  <ion-toolbar>
    <ion-title>
      マップを見るよー
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true">

<div #map id="map"></div>

</ion-content>
home.page.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';

declare var google;

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})

export class HomePage implements OnInit {

// Map関係
  @ViewChild('map', { static: true }) mapElement: ElementRef;
  map: any;
  markers = [];

constructor() { }

ngOnInit() {
    console.log('ngOnInitです');
  }

  ionViewWillEnter() {
    this.loadMap();
  }

// Initialize a blank map
  loadMap() {
    let latLng = new google.maps.LatLng(51.9036442, 7.6673267);
    let mapOptions = {
      center: latLng,
      zoom: 5,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    };
    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
  }

}
home.page.scss
#map {
  width: 100%;
  height: 70%;
}

ブラウザで確認

ionic serveを実行!

スクリーンショット 2020-05-19 22.58.20.jpg

最後に

こちらを参考にやりましたが、APIキーの部分でつまずいたので、、、:unamused:
Google MapのAPIキー取得はやったことあるのに、完全に忘れてしまった。。。

3
0
3

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
3
0