Im using Tab Page in my ionic App, at first my maps will not shows up when i change to another tab and then go back to map page again. And then i found workaround on internet and my map did shows up again after i change to another tab. But the problem is now the map is freeze, i cant drag, move or zoom my maps. Here is my code to shows the map.
import { Component , ViewChild, ElementRef} from '@angular/core';
import { NavController } from 'ionic-angular';
import {GoogleMap, GoogleMaps, LatLng, CameraPosition, GoogleMapsEvent , Marker, MarkerOptions } from '@ionic-native/google-maps';
import {Geolocation} from '@ionic-native/geolocation';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('map') mapelement : ElementRef;
map : GoogleMap;
initialMapLoad: boolean = true;
constructor(public navCtrl: NavController, private _googlemap : GoogleMaps,
private _geolocation : Geolocation) {
}
ionViewDidLoad(){
this.initMap();
}
initMap(){
let element = this.mapelement.nativeElement;
this.getlocation().then(res => {
let loc = new LatLng(res.coords.latitude, res.coords.longitude);
this.map = this._googlemap.create('map', {
camera:{
target: loc,
zoom: 15
}
});
this.createmarker(loc, 'Me');
}).catch (err => {
console.log(err);
});
}
ionViewDidEnter(){
if (!this.initialMapLoad) {
this.map.setDiv('map');
} else {
this.initialMapLoad = false;
}
}
getlocation(){
return this._geolocation.getCurrentPosition();
}
movecamer(loc : LatLng){
let options : CameraPosition<LatLng> ={
target: loc,
zoom: 15,
tilt: 10
}
this.map.moveCamera(options)
}
createmarker(loc: LatLng, title : string){
let markeroptions : MarkerOptions = {
position : loc,
title : title
}
return this.map.addMarker(markeroptions);
}
}
Can anyone help me ? and tell me what is wrong here.
Why map is freeze and I cant move the map ? thanks
I solved the problem by removing the watchPosition event , just call the getCurrentPosition event in promise and it will solve the tab freezing problem.
this.geolocation.getCurrentPosition().then((resp) => {
// resp.coords.latitude
// resp.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
});