Hi All,
I have the following implementation of Google Maps api (see below). It works perfectly when I run ionic serve
and test it in a browser. However, when I do cordova build
and deploy the apk to my android 6.0.1 phone, it fails. The map does not load and it hangs on the loading spinner.
If anyone has any ideas, I would appreciate the help.
Thanks
map.ts
import { Component } from '@angular/core'; import { Geolocation } from 'ionic-native'; import { NavController, Loading } from 'ionic-angular';
@Component({ templateUrl: 'build/pages/map/map.html', }) export class MapPage {
private map: any; private google: any; private markers = []; private loading: Loading = Loading.create();
constructor(private nav: NavController) { this.map = null; this.loadMap(); }
loadMap() { this.nav.present(this.loading); let options = { timeout: 10000, enableHighAccuracy: true };
Geolocation.getCurrentPosition(options).then((position) => { let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
let mapOptions = { center: latLng, zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP };
this.map = new google.maps.Map(document.getElementById("map"), mapOptions);
this.map.addListener('click', (event) => { this.addMarker(event.latLng); });
this.loading.dismiss();
}); }
addInfoWindow(marker, content: string) { let infoWindow = new google.maps.InfoWindow({ content: content }); google.maps.event.addListener(marker, 'click', function () { infoWindow.open(this.map, marker); }) }
addMarker(location) { if (!location) { location = this.map.getCenter(); } let marker = new google.maps.Marker({ map: this.map, animation: google.maps.Animation.DROP, position: location });
this.markers.push(marker);
let content: string = 'remove'; this.addInfoWindow(marker, content); }
deleteAllMarkers() { this.clearMarkers(); this.markers = []; }
undoLastMarker() { let marker = this.markers.pop() if (marker) { marker.setMap(null); } }
setMapOnAll(map) { for (var i = 0; i < this.markers.length; i++) { this.markers[i].setMap(map); } }
clearMarkers() { this.setMapOnAll(null); }
}
map.html
<ion-header> <ion-navbar> <button menuToggle> <ion-icon name="menu"></ion-icon></button> <ion-title>Map</ion-title> <ion-nav-items> <button (click)="addMarker()" danger>Add Marker <ion-icon class="ion-ios-pin"></ion-icon></button> <button (click)="undoLastMarker()" light>Undo <ion-icon class="ion-ios-undo"></ion-icon></button> <button (click)="deleteAllMarkers()" light>Clear <ion-icon class="ion-ios-trash"></ion-icon></button> </ion-nav-items> </ion-navbar> </ion-header>
<ion-content padding>
<div id="map"></div>
</ion-content>
index.html
<!-- google maps NOTE: When building production applications, you should also obtain an API key for your Google Maps integration and supply it as a parameter in the query string (i.e ?key=yourkey) --> <script src="http://maps.google.com/maps/api/js?sensor=true"></script>
I do get the following warnings in the browser console, but I thought I only need a key in when I go live. Could this be the problem? Do I need the api key?
Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
Google Maps API warning: SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required