javascript - Chamar método MarkerClusterer do GoogleMap a partir de um controlador angular baseado em TypeScript
Comecei a investigar a abordagem do TypeScript no meu projeto e atualmente estou um pouco confuso sobre como organizar corretamente a chamada ao método MarkerClusterer. Atualmente, tenho referências de definições de tipo:
///<reference path="../../typings/angularjs/angular.d.ts" />
///<reference path="../../typings/google.maps.d.ts" />
Mas para o MarkerClusterer js eu não consegui encontrar uma biblioteca de definição ts. Meu código agora está assim:
class paspController {
public map: any;
public markers;
public mapTab: boolean;
public currentId: number;
//Some code
showTab(tabIndex: number) {
if (tabIndex == 2) {
this.mapTab = true;
var that = this;
setTimeout(function () {
this.options = {
zoom: 2,
center: new google.maps.LatLng(1, 1),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
if (!that.map) {
that.map = new google.maps.Map(document.getElementById('map'), this.options);
}
jQuery.ajax({
type: "GET",
url: 'GetDivesWithCoordinates/' + that.currentId,
success: function (data) {
that.markers = [];
var marker;
for (var i = 0; i < data.length; i++) {
marker = new google.maps.Marker({ map: that.map, draggable: false, title: data[i].Location + ": " + data[i].DiveComment, position: new google.maps.LatLng(data[i].CoordinateX, data[i].CoordinateY) });
that.markers.push(marker);
}
// THIS IS MY PROBLEM => var markerCluster = new MarkerClusterer(that.map, markers);
},
error: function (e) {
},
async: false
});
}, 100);
}
}
Como devo chamar corretamente o MarkerClusterer, ou talvez eu deva colocá-lo fora da lógica do controlador?
Resposta #1
ESTE É MEU PROBLEMA => var markerCluster=new MarkerClusterer(that.map, markers);
Declare isso:
declare var MarkerClusterer:any;
E o TypeScript não reclamará mais.
Mais: http://basarat.gitbooks.io/typescript/content/docs/types/migrating.html
Resposta #2
Use o pacote npm abaixo em vez de adicionar scripts diretamente no Angular.
Adicione este pacote ao seu node_modules
npm i @googlemaps/markerclustererplus
No seu arquivo Ts importe isto:
import MarkerClusterer from '@googlemaps/markerclustererplus';
const markerCluster = new MarkerClusterer(map, markers);
Referência: https://github.com/googlemaps/js-markerclustererplus
