1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| export class AppComponent {
title = "Google-map";
// données markers
dataMarkers = [
{ "lat" :44.837368, "lng" :-0.576144, "title" :"Bordeaux", "info" :"<b>Bordeaux<\/b><br>la suite du texte..."},
{ "lat" :45.194276, "lng" :5.731633, "title" :"Grenoble", "info" :"<b>Grenoble<\/b><br>la suite du texte..."},
{ "lat" :43.297612, "lng" : 5.381042, "title" :"Marseille", "info" :"<b>Marseille<\/b><br>la suite du texte..."},
{ "lat" :45.767299, "lng" : 4.834329, "title" :"Lyon", "info" :"<b>Lyon<\/b><br>la suite du texte..."},
{ "lat" :48.856667, "lng" : 2.350987, "title" :"Paris", "info" :"<b>Paris<\/b><br>la suite du texte..."}
];
// pour gestion éventuelle
markers = [];
@ViewChild("carte") gmapElement: any;
map: google.maps.Map;
infoWindow: google.maps.InfoWindow;
// affichage infoWindow
showInfoWindow( marker){
let content = marker.get("info");
this.infoWindow.setContent(content);
this.infoWindow.open( marker.getMap(), marker);
}
// création d'un marker
addMarker(map, options) {
let oMarker = new google.maps.Marker({
"position": options.position || map.getCenter(),
"map": map,
"zIndex": 0
});
// ajout data infoWindow because typescript
oMarker.set("info", options.info || "");
// ajout événement
// attention au scope
let me = this;
google.maps.event.addListener(oMarker, "click", function (data) {
me.showInfoWindow( this); // this ici est le oMarker
});
// pour gestion
return oMarker;
}
ngOnInit() {
// création de la carte
this.map = new google.maps.Map(this.gmapElement.nativeElement, {
"backgroundColor": "#FFF",
"streetViewControl": false,
"zoom": 6,
"minZoom": 2,
"mapTypeControl": false,
"gestureHandling": "greedy",
"center": {
"lat": 46.8,
"lng": 1.7
},
"mapTypeId": google.maps.MapTypeId.TERRAIN
});
// création infoWindow
this.infoWindow = new google.maps.InfoWindow({
content: "Rien pour le moment"
});
// création des markers
this.dataMarkers.forEach( (elem) => {
// ajout d'un marker
this.markers.push(
this.addMarker( this.map, {
"position": elem,
"title" : elem.title,
"info" : elem.info
})
);
});
}
} |
Partager