В настоящее время работаю над мобильным приложением ionic 4, мне нужно добавить карту для взаимодействия.Когда я запускаю приложение, загрузка работает нормально, но когда я хочу взаимодействовать с картой другими методами, кроме загрузки, моя переменная не определена.
В этой части вы увидите две службы, которыеобеспокоены этой проблемой.В итоге, когда я запускаю приложение, я вызываю функцию loadMap () и после этого я вызываю метод addMarker ().
Именно в этом методе моя карта переменных не определена, и я не понимаю,почему.
Это сервис, который вызывает метод addMarker ()
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { getRandomString } from 'selenium-webdriver/safari';
import {Marker, MarkerOptions} from '@ionic-native/google-maps';
import {MapService} from './map.service';
const GlobalIcon = 'compass';
@Injectable({
providedIn: 'root'
})
export class IndiceService {
public indices: Array<{ title: string; note: any; icon: string, id: number, lat:number, lng:number }> = [];
constructor(private router: Router, private map : MapService) { }
ajouterIndice(passedNote:any){ //Faire cet ajout à l'aide d'une promesse ???
this.indices.push({
title : 'Indice ' + (this.indices.length+1),
note : passedNote,
icon : GlobalIcon,
id : this.indices.length+1,
lat: this.getRandomInt(45,75),
lng: this.getRandomInt(4,5),
});
this.map.addMarker(this.indices.length,this.indices[this.indices.length-1].note,this.indices[this.indices.length-1].lat,this.indices[this.indices.length-1].lng);
this.router.navigate(['/home']);
}
isIndiceIncluded(data :any) : boolean{
return this.indices.find(x => x.note === data) != undefined;
}
getRandomInt(min, max) : number {
return Math.random() * (max - min + 1) + min;
}
}
Этот сервис является моим картографическим сервисом
import { Injectable } from '@angular/core';
import {
GoogleMaps,
GoogleMap,
GoogleMapsEvent,
GoogleMapOptions,
CameraPosition,
MarkerOptions,
Marker,
Environment
} from '@ionic-native/google-maps';
@Injectable({
providedIn: 'root'
})
export class MapService {
private map: GoogleMap;
public markers : Array<Marker> = [];
constructor() { }
loadMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: 43.783125,
lng: 2.809419
},
zoom: 18,
tilt: 30
}
};
this.map = GoogleMaps.create('map', mapOptions);
let marker: Marker = this.map.addMarkerSync({
title: 'Ionic',
icon: 'red',
animation: 'DROP',
position: {
lat: 45.783125,
lng: 4.809419
},
});
/*marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
alert('clicked');
});*/
}
addMarker(id : number ,note : any, lat:number, lng:number){
console.log(this.map); //write undefined
let currentMarker : Marker = this.map.addMarkerSync({
id : id,
title: note,
icon: 'red',
animation: 'DROP',
position: {
lat: lat,
lng: lng
},
});
this.markers.push(currentMarker);
}
}
Заранее благодарим за вашу помощь иизвините за мой уровень английского.