首页 > 解决方案 > 我如何通过插件获取 lat long 值,而不是在角度 5 中调用 ionic 中的 Web 服务?

问题描述

我试图通过在离子框架中按角度 5 的地理编码给出位置来找出纬度。在找到 lat long 后,我试图通过在服务中传递 lat long 来调用我的服务。

问题是我的服务在插件调用之前被调用,因此我得到了未定义的 lat long 的值。

//this is the plugin
    selectSearchResult(item) {
        var address =item.description;
        this.geocoder.geocode({ 'address': address}, function(results, status) {
          if (status == 'OK') {
            this.formatted_address=results[0].formatted_address;
            console.log(this.formatted_address);
             this.latitude = results[0].geometry.location.lat();
             this.longitude = results[0].geometry.location.lng();
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
           }
         });
    ////this is my service
         this.fetch.GetLocation(this.latitude,this.longitude).subscribe(
          result => {
            console.log(result);
          },
          err =>{
            console.error("Error : "+err);
          } ,
          () => {
            console.log('getData completed');
          }
        );
       }

我如何在我的网络服务之前调用插件?

标签: angularionic-frameworkgeocode

解决方案


对于您的代码,可以尝试如下:

selectSearchResult = (item) => {
        var address =item.description;
        this.geocoder.geocode({ 'address': address}, function(results, status) {
            if (status == 'OK') {
            this.formatted_address=results[0].formatted_address;
            console.log(this.formatted_address);
             this.latitude = results[0].geometry.location.lat();
             this.longitude = results[0].geometry.location.lng();
             //web service function call
             this.serviceFun();
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
           }
        });
    } 

    serviceFun = () => {
     this.fetch.GetLocation(this.latitude,this.longitude).subscribe(
      result => {
        console.log(result);
      },
      err =>{
        console.error("Error : "+err);
      } ,
      () => {
        console.log('getData completed');
      }
    );
   }

推荐阅读