I have a map component utilizing the mapbox API that is dependent upon the longitudinal and latitudinal coordinates returned from a geocoder.
There is a service that calls an endpoint with some parameters. I subscribe to this service and use an arrow function as seen in the code below. While I expect the response to have been assigned to this.geocodeResponse as well as the respective this.longitude and this.latitude, these variables remain empty outside the function scope. I know this because I receive this response from the broswer ERROR Error: "Invalid LngLat object: (NaN, NaN)". I have corroborated this result by logging the responses to console.log and confirm that the three responses remain empty.
I know that the service works because it has been tested and produces a valid response elsewhere.
Here is the relevant code:
import { Component, OnInit, ViewChild, ElementRef, Input } from "@angular/core";
import mapboxgl from "mapbox-gl";
import { GeocodingService } from "../geocoding.service";
import { environment } from "../../../environments/environment";
@Component({
  selector: "app-dynamic-map",
  templateUrl: "./dynamic-map.component.html",
  styleUrls: ["./dynamic-map.component.css"]
})
export class DynamicMapComponent implements OnInit {
  @Input() queryString: String
  geocodeResponse: any;
  longitude: string;
  latitude: string;
  map: mapboxgl.Map;
  @ViewChild("mapElement") mapElement: ElementRef;
  constructor(private geocodingService: GeocodingService) {}
  ngOnInit() {
    mapboxgl.accessToken = environment.mapbox.accessToken;
    this.geocodingService.getGeoCords(this.queryString).subscribe(response => {
      this.geocodeResponse = response;
      this.longitude = this.geocodeResponse.features[0].bbox[0],
      this.latitude = this.geocodeResponse.features[0].bbox[1]
  });
  }
  /* Given a query string, get the first result of a places query from api and pass to
   * the dynamic map centering on coordinates */
  ngAfterViewInit() {
    /* THESE REMAIN UNASSIGNED HERE */
    console.log(this.geocodeResponse)
    console.log(this.longitude)
    console.log(this.latitude)
    this.map = new mapboxgl.Map({
      container: this.mapElement.nativeElement,
      style: "mapbox://styles/mapbox/streets-v9",
      center: [this.longitude, this.latitude],
      zoom: 12
    });
  }
}
I know the issue resides within the subscription to the service. I may not fully understand how Angular handles subscribe and the scoping of the response variable.
The expected behavior would be non-empty variables outside of the subscription response. Moreover, I have observed this behavior within the firefox debugger, but without the tags having been triggered within the debugger, the variables remain unassigned.
I can provide additional code and logging output if needed. Please be specific in what is needed to diagnose the problem. Furthermore, while an explicit resolution to the problem is nice, I would really like to understand why this problem is occurring because I have a few other components that have similar issues.
