无法使Google Maps附近的函数与回调一起使用

时间:2019-05-07 12:57:03

标签: angular typescript google-maps callback angular-google-maps

因此,我必须更新以前使用setTimeout而不是Callbacks编写的一些TypeScript代码,问题是我无法使其正常工作。

使用settimeouts的函数可以正常工作,因此没有理由与配置相关(库=场所等)。另外,我可以在日志中看到它确实在工作,但是我的地图是纯灰色的。

map_initialize(callback) {
    navigator.geolocation.getCurrentPosition((position) => {      

      this.directionsService = new google.maps.DirectionsService();
      this.localizacaoAtual = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

      var mapOptions = {
        zoom: 8,
        center: this.localizacaoAtual
      }

      if (this.origem == "atual") {
        this.origem = this.localizacaoAtual;
      }

      callback({ mapOptions: mapOptions })
    });
  }

现在我要调用函数initialize

if (this.origem != null && this.destino != null) {

        this.map_initialize((callback) => {

          if (this.tipoMapa == "rota") {
            this.map_calcRoute(callback);

          } else if (this.tipoMapa == "radius") {

            this.map_radius(callback);
          }
        });
    }

如果要调用的第一个函数是路线图,它实际上是有效的。但是,如果我将“ this.tipoMapa”值更改为“ radius”,则不会加载。

map_radius(mapOptions: any) {
    this.map = new google.maps.Map(document.getElementById('map'), mapOptions);
    let service = new google.maps.places.PlacesService(this.map);
    let start = this.origem;
    let end = this.destino;

      this.request = {
        location: start,
        radius: '1500',
        name: ['something here']
      }

    service.nearbySearch(this.request, (results, status) => {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        var stepDisplay: any = new google.maps.InfoWindow();
        for (var i = 0; i < results.length; i++) {
          this.createMarker(results[i], stepDisplay);
        }
      }
        this.entrada = { mensagem: status, origem: this.origem, destino: this.destino, instrucoes: this.instrucoes };
        this.conversationService.sendMessage(this.entrada, this.context).subscribe(
          data => {
            this.conv_tratarRetorno(data);
          }, error => { }
        )
    });
  };

然后我有了我的createMarker函数。

createMarker(place, stepdisp) {

    var marker = new google.maps.Marker({
      map: this.map,
      position: place.geometry.location
    });

    google.maps.event.addListener(marker, 'click', function () {

      stepdisp.setContent(place.name);
      stepdisp.open(this.map, marker);
    });
  }

因此,基本上,当我调用route函数时,它确实可以工作,但是如果我调用radius函数,它将返回灰色地图,但带有正确的日志。

谢谢!

1 个答案:

答案 0 :(得分:0)

解决了!我正在调用“ mapOptions”,却忘记了调用mapOptions.mapOptions。愚蠢的我...