代码之家  ›  专栏  ›  技术社区  ›  Nancy

如何在Angular中使用google距离矩阵API

  •  0
  • Nancy  · 技术社区  · 6 年前

    我正在尝试使用谷歌距离矩阵API。 https://developers.google.com/maps/documentation/distance-matrix/start 我有必要的请求参数,这些参数是从当前位置(原点纬度和液化天然气)获得的,当我点击标记时(我得到目的地纬度和液化天然气)。现在我试图调用服务文件中的API,它不能正常工作。

    以下是服务代码: 下面是sendQuery对象中传递的请求参数。

    apiKeyToPass: "this will be apikey"
    srcDestinationLat: 29.9809683
    srcDestinationLng: 31.3377553
    srcOriginLat: 11.127122499999999
    srcOriginLng: 78.6568942
    

    我将传递apikey来代替apikey。

    getDistanceMatrix(sendQuery): Observable<any> {
           return this.http.get('https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={{srcOriginLat}},{{srcOriginLng}}&destinations={{srcDestinationLat}},{{srcDestinationLng}}&key=apikey') 
        .map((response: Response) => {
            return response.json();
          })
          .catch(this.handleError);
      }
    

    当我执行上述代码时,代码运行到异常循环中,而不是进入响应循环。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Janier Hernandez    6 年前

    我的工作是:

    public getDistancia(origen: string, destino: string) {
        return new google.maps.DistanceMatrixService().getDistanceMatrix({'origins': [origen], 'destinations': [destino], travelMode: 'DRIVING'}, (results: any) => {
            console.log('resultados distancia (mts) -- ', results.rows[0].elements[0].distance.value)
        });
    }