Variables scopes

Hi,

I’ve been using the cordova-plugin-googlemaps and when I create the map and I add markers like this:

loadMap(){
      let location = new GoogleMapsLatLng(LAT_POSITION_X,LNG_POSITION_X);
	  
      this.map = new GoogleMap('map', {
          'backgroundColor': 'white',
          'controls': {
            'compass': true,
            'myLocationButton': true,
            'indoorPicker': true,
            'zoom': true
          },
          'gestures': {
            'scroll': true,
            'tilt': true,
            'rotate': true,
            'zoom': true
          },
          'camera': {
            'latLng': location,
            'tilt': 30,
            'zoom': 15,
            'bearing': 50
          }
      });
 
      this.map.on(GoogleMapsEvent.MAP_READY).subscribe(() => {

         // create new marker
        let markerOptions: GoogleMapsMarkerOptions = {
          position: location,
          title: 'Ionic'
        };

        this.map.addMarker(markerOptions)
          .then((marker: GoogleMapsMarker) => {
            marker.showInfoWindow();
          });		  
      });
	}
  }

It works, but when I add the getCurrentPosition function like this:

loadMap(){
    function onSuccess(position) {
      let location = new GoogleMapsLatLng(position.coords.latitude,position.coords.longitude);
	  
      this.map = new GoogleMap('map', {
          'backgroundColor': 'white',
          'controls': {
            'compass': true,
            'myLocationButton': true,
            'indoorPicker': true,
            'zoom': true
          },
          'gestures': {
            'scroll': true,
            'tilt': true,
            'rotate': true,
            'zoom': true
          },
          'camera': {
            'latLng': location,
            'tilt': 30,
            'zoom': 15,
            'bearing': 50
          }
      });
 
      this.map.on(GoogleMapsEvent.MAP_READY).subscribe(() => {

         // create new marker
        let markerOptions: GoogleMapsMarkerOptions = {
          position: location,
          title: 'Ionic'
        };

        this.map.addMarker(markerOptions)
          .then((marker: GoogleMapsMarker) => {
            marker.showInfoWindow();
          });    		  
      });
	}
	
	function onError(error) {
		alert('code: '    + error.code    + '\n' +
			  'message: ' + error.message + '\n');
	}
	 
	navigator.geolocation.getCurrentPosition(onSuccess, onError);
  }

The the marker doesn’t show, where I can see why the location variable doesn’t work? because in this page https://www.typescriptlang.org/docs/handbook/variable-declarations.html I do not see that.

Thanks!

JavaScript’s concept of this is extremely counterintuitive.

1 Like