# Calling a function from a listener

**URL:** <https://forum.ionicframework.com/t/calling-a-function-from-a-listener/58814>\
**Category:** ionic-v3\
**Created:** [July 28, 2016, 7:31am UTC](https://forum.ionicframework.com/t/calling-a-function-from-a-listener/58814 "2016-07-28T07:31:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardmarais](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardmarais/32/101456_2.png) [@richardmarais](https://forum.ionicframework.com/u/richardmarais)\
**Post date:** [July 28, 2016, 7:31am UTC](https://forum.ionicframework.com/t/calling-a-function-from-a-listener/58814/1 "2016-07-28T07:31:02Z")

</div>

Hi All,

It is probably simple javascript, but I am no expert. Can anyone assist me how I can call the function `addMarker` below from within the event listener? I am not sure why it can’t see the function.

Thanks

error:

```
TypeError: this.addMarker is not a function
this.addMarker(event.latLng);

```

code:

> ```
> loadMap() {
> .....
> this.map.addListener('click', function (event) {
> this.addMarker(event.latLng);
> });
> }
> 
> ```

> ```
> addMarker(location) {
> .....
> }
> 
> ```

This is the entire ts file:

> ```
> import { Component } from '@angular/core';
> import { Geolocation } from 'ionic-native';
> import { NavController } from 'ionic-angular';
> 
> ```

> ```
> @Component({
> templateUrl: 'build/pages/map/map.html',
> })
> export class MapPage {
> 
> ```

> ```
> private map: any;
> private google: any;
> private markers = [];
> 
> ```

> ```
> constructor(private nav: NavController) {
> this.map = null;
> this.loadMap();
> }
> 
> ```

> ```
> loadMap() {
> let options = {
> timeout: 10000,
> enableHighAccuracy: true
> };
> 
> ```

> ```
> Geolocation.getCurrentPosition(options).then((position) => {
> let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
> 
> ```

> ```
> let mapOptions = {
> center: latLng,
> zoom: 15,
> mapTypeId: google.maps.MapTypeId.ROADMAP
> };
> 
> ```

> ```
> this.map = new google.maps.Map(document.getElementById("map"), mapOptions);
> 
> ```

> ```
> // this.map.addListener('click', function (event) {
> // this.addMarker(event.latLng);
> // });
> 
> ```

> ```
> google.maps.event.addListener(this.map, 'click', function (event) {
> this.addMarker(event.latLng);
> })
> 
> ```

> ```
> });
> }
> 
> ```

> ```
> addInfoWindow(marker, content: string) {
> let infoWindow = new google.maps.InfoWindow({
> content: content
> });
> google.maps.event.addListener(marker, 'click', function () {
> infoWindow.open(this.map, marker);
> })
> }
> 
> ```

> ```
> addMarker(location) {
> if (!location) {
> location = this.map.getCenter();
> }
> let marker = new google.maps.Marker({
> map: this.map,
> animation: google.maps.Animation.DROP,
> position: location
> });
> 
> ```

> ```
> this.markers.push(marker);
> 
> ```

> ```
> let content: string = 'remove';
> this.addInfoWindow(marker, content);
> }
> 
> ```

> ```
> deleteAllMarkers() {
> this.clearMarkers();
> this.markers = [];
> }
> 
> ```

> ```
> undoLastMarker() {
> let marker = this.markers.pop()
> if (marker) {
> marker.setMap(null);
> }
> }
> 
> ```

> ```
> setMapOnAll(map) {
> for (var i = 0; i < this.markers.length; i++) {
> this.markers[i].setMap(map);
> }
> }
> 
> ```

> ```
> clearMarkers() {
> this.setMapOnAll(null);
> }
> 
> ```

> ```
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![xr0master](https://avatars.discourse-cdn.com/v4/letter/x/b4bc9f/32.png) [@xr0master](https://forum.ionicframework.com/u/xr0master)\
**Post date:** [July 28, 2016, 7:53am UTC](https://forum.ionicframework.com/t/calling-a-function-from-a-listener/58814/2 "2016-07-28T07:53:52Z")

</div>

```auto
this.map.addListener('click', (event) => {
    this.addMarker(event.latLng);
});

```

---

<div class="post-metadata">

**Author:** ![richardmarais](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/richardmarais/32/101456_2.png) [@richardmarais](https://forum.ionicframework.com/u/richardmarais)\
**Post date:** [July 28, 2016, 7:56am UTC](https://forum.ionicframework.com/t/calling-a-function-from-a-listener/58814/3 "2016-07-28T07:56:20Z")

</div>

Thank you, that works
