Ionic 3 Google Maps Integration

I am following this tutorial exactly: https://www.djamware.com/post/58f4da2080aca7414e78a638/step-by-step-tutorial-of-ionic-3-angular-4-and-google-maps-directions-service

But I cannot get it to work. I don't have an API key, but for some reason I keep getting the errorError: Uncaught (in promise): ReferenceError: google is not defined ReferenceError: google is not defined

I am launching the application using ionic lab

For some reason, it doesn't work. Can anyone help me find the problem? I tried adding the whostelist cordova plugin by changing https to http in the key part of the API, but still it doesn't work.

+3


source to share


2 answers


Have you declared a variable at home.ts?



import { Component, ViewChild, ElementRef } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { NavController } from 'ionic-angular';

declare var google;

@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
...

      

+1


source


Ionic 3 Google Map click doc link

1. Install this package

$ ionic cordova plugin add https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"
$ npm install --save @ionic-native/google-maps

      

2.Android and ios Api create Go to this site - Google cloud platform

3.After you have installed the Api packages

4.Import app.module.ts



import { GoogleMaps } from '@ionic-native/google-maps';
......
provider:[
 GoogleMaps
];

      

5.home.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <div id="map"></div>
</ion-content>

      

6.home.ts

import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';

import {
  GoogleMaps,
  GoogleMap,
  GoogleMapsEvent,
  LatLng,
  CameraPosition,
  MarkerOptions,
  Marker
 } from '@ionic-native/google-maps';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController,
    private googleMaps: GoogleMaps,
    public platform: Platform) {
      platform.ready().then(()=>{
        this.loadMap();
      })

  }

   loadMap() {

    // create a new map by passing HTMLElement
    let element: HTMLElement = document.getElementById('map');

    let map: GoogleMap = this.googleMaps.create(element);

    // listen to MAP_READY event
    // You must wait for this event to fire before adding something to the map or modifying it in anyway
    map.one(GoogleMapsEvent.MAP_READY).then(
      () => {
        console.log('Map is ready!');
        // Now you can add elements to the map like the marker
      }
    );

    // create CameraPosition
    let position: CameraPosition = {
      target: {
        lat: 43.0741904,
        lng: -89.3809802
      },
      zoom: 18,
      tilt: 30
    };

    // move the map camera to position
    map.moveCamera(position);

    // create new marker
    let markerOptions: MarkerOptions = {
      //position: ionic,
      title: 'Ionic'
    };

    map.addMarker(markerOptions)
      .then((marker: Marker) => {
         marker.showInfoWindow();
       });
    }
}

      

+1


source







All Articles