angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

AGM mapClick($event) not working properly

Open markolo opened this issue 6 years ago • 21 comments

I made the Getting Started AGM tutorial: https://angular-maps.com/guides/getting-started/

I installed: "@angular/core": "~10.0.0" and "@agm/core": "^3.0.0-beta.0"

In app.component.html I added: <agm-map [latitude]="lat" [longitude]="lng" (mapClick)="mapClick($event)"> <agm-marker [latitude]="lat" [longitude]="lng">

In app.component.ts I added: export class AppComponent { title = 'gbis'; lat = 51.678418; lng = 7.809007;

mapClick(e) {
    console.log(e);
}

}

In app.component.css I added: agm-map { height: 300px; }

When I run this app the map is working but if I click on the map in TS file function mapClick(e) I receive e="c". No coordinates recieved from parameter e.

please help!

markolo avatar Jul 24 '20 10:07 markolo

ETA on this fix? or workaround?

amalmo avatar Jul 27 '20 11:07 amalmo

I encountered exactly the same issue.

sqaliu avatar Jul 30 '20 19:07 sqaliu

Could anybody fix this issue?

CesarIAndrade avatar Aug 02 '20 06:08 CesarIAndrade

same issue, have to use an older version to workaround this?

gggiiia avatar Aug 03 '20 18:08 gggiiia

Could anybody fix this issue?

juanreverter avatar Aug 03 '20 20:08 juanreverter

Could anybody fix this issue?

i suggest to use an older version for now, since this bug is critical, negating basic interaction with the map

gggiiia avatar Aug 03 '20 21:08 gggiiia

Could anybody fix this issue?

i suggest to use an older version for now, since this bug is critical, negating basic interaction with the map

Yes, the previous version 1.1.0 (8 months ago) works fine.

juanreverter avatar Aug 04 '20 00:08 juanreverter

simple fix at #1847, waiting for a review from @SebastianM

ghost avatar Aug 04 '20 09:08 ghost

I am encountering the same problem. $event in mapClick returns a string "c". Any idea when this will be fixed? Any solution other than shifting to an older version?

AsadAmir111 avatar Aug 10 '20 12:08 AsadAmir111

As workaround you can add listener to the google map instance

<agm-map [latitude]="position.lat"
         [longitude]="position.lng"
         [zoom]="zoom"
         (mapReady)="mapReadyHandler($event)">
</agm-map>
public mapReadyHandler(map: google.maps.Map): void {
  this.map = map;
  this.mapClickListener = this.map.addListener('click', (e: google.maps.MouseEvent) => {
    this.zone.run(() => {
      // Here we can get correct event
      console.log(e.latLng.lat(), e.latLng.lng());
    });
  });
}

public ngOnDestroy(): void {
  if (this.mapClickListener) {
    this.mapClickListener.remove();
  }
}

egorkel-da14 avatar Aug 11 '20 16:08 egorkel-da14

¿Alguien podría solucionar este problema?

sugiero usar una versión anterior por ahora, ya que este error es crítico, negando la interacción básica con el mapa

Sí, la versión anterior 1.1.0 (hace 8 meses) funciona bien.

Si funciona con la versión 1.1.0, usar el comando npm i @agm/[email protected]

jesuschavez1994 avatar Sep 02 '20 01:09 jesuschavez1994

When will this be released?

ahmadalfy avatar Oct 12 '20 00:10 ahmadalfy

Having the same issue on "@agm/core": "^3.0.0-beta.0"

paishin avatar Dec 14 '20 17:12 paishin

@egorkel-da14 I am trying your workaround but getting this message: Uncaught TypeError: Cannot read property 'run' of undefined when calling this.zone.run(...)

Am I missing something here?

update: It seems that I can get the coordinates when omitting that line:

    public mapReadyHandler(map: google.maps.Map): void {
        this.map = map;
        this.mapClickListener = this.map.addListener('click', (e: google.maps.MouseEvent) => {
            console.log(e.latLng.lat(), e.latLng.lng());
        });
    }

paishin avatar Dec 14 '20 18:12 paishin

@egorkel-da14 I am trying your workaround but getting this message: Uncaught TypeError: Cannot read property 'run' of undefined when calling this.zone.run(...)

Am I missing something here?

update: It seems that I can get the coordinates when omitting that line:

    public mapReadyHandler(map: google.maps.Map): void {
        this.map = map;
        this.mapClickListener = this.map.addListener('click', (e: google.maps.MouseEvent) => {
            console.log(e.latLng.lat(), e.latLng.lng());
        });
    }

Yes :) you need to inject NgZone service to the constructor

constructor(private zone: NgZone) {}

egorkel-da14 avatar Dec 14 '20 18:12 egorkel-da14

You need this line for proper work of Angular change detection (for proper view update) @paishin

egorkel-da14 avatar Dec 14 '20 18:12 egorkel-da14

call event.latLng.lat() working markerDragEnd($event: any) { console.log($event.latLng.lat(), $event.latLng.lng()); }

bug4o4 avatar Jan 29 '21 13:01 bug4o4

I have the same issue with addPin function, but I need to use version 3.0.0 to have translation for maps according user locale. So I need to know is version with translation and add pin functionality was published earlier or not? (because in version 1.1.0 translate still not available)

panimat avatar Feb 03 '21 13:02 panimat

¿Alguien podría solucionar este problema?

sugiero usar una versión anterior por ahora, ya que este error es crítico, negando la interacción básica con el mapa

Sí, la versión anterior 1.1.0 (hace 8 meses) funciona bien.

Si funciona con la versión 1.1.0, usar el comando npm i @agm/[email protected]

With Angular 10 ?

munozdaniel avatar Mar 18 '21 17:03 munozdaniel

As workaround you can add listener to the google map instance

<agm-map [latitude]="position.lat"
         [longitude]="position.lng"
         [zoom]="zoom"
         (mapReady)="mapReadyHandler($event)">
</agm-map>
public mapReadyHandler(map: google.maps.Map): void {
  this.map = map;
  this.mapClickListener = this.map.addListener('click', (e: google.maps.MouseEvent) => {
    this.zone.run(() => {
      // Here we can get correct event
      console.log(e.latLng.lat(), e.latLng.lng());
    });
  });
}

public ngOnDestroy(): void {
  if (this.mapClickListener) {
    this.mapClickListener.remove();
  }
}

Thank you so much for this code, it's verry helpful, have great day

sir-amrouz avatar Mar 29 '21 10:03 sir-amrouz