InvalidValueError:属性来源中:不是字符串;而不是LatLng或LatLngLiteral:不是对象;不是对象

时间:2018-09-10 14:46:24

标签: angular google-maps

我正在使用谷歌角度地图来设置当前位置,并使用agm-direction来设置方向。下面是代码,我遇到了上述错误。

3 个答案:

答案 0 :(得分:0)

这可能是因为调用lat时没有填充组件的lnggetDirections()getUserLocation()将不会同步生成位置坐标。您可以通过粘贴在浏览器控制台中来检查getUserLocation()下的代码。您会发现分配职位需要时间。如果您在给出结果所需的时间之间致电getDirections(),则会收到上述错误。

我这样制作了getDirections()

async getDirection() {
    if (typeof this.lat == "undefined" || typeof this.lng == "undefined" || typeof this.zoom == "undefined") {
      await this.getUserLocation();
    }
    // code below will not be executed until getUserLocation() is complete
    this.origin = { lat: this.lat, lng: this.lng };
    this.destination = { lat: 24.799524, lng: 120.975017 };
} 

您的getUserLocation()将最多被调用两次。

答案 1 :(得分:0)

注意:确保在将数据设置为lat,lon变量后调用get get direction以避免竞争情况。并像下面的代码片段一样将值解析为Float

 getDirection() {
    this.origin = { lat: parseFloat(this.startLat), lng: parseFloat(this.startLon) };
    this.destination = { lat:parseFloat(this.endLat), lng: parseFloat(this.endLon) };


    console.log(this.origin, this.destination);

  }

答案 2 :(得分:0)

对于我来说,在Directions Service的JS Map API示例中,我复制了此代码:

origin: {
    query: document.getElementById("start").value,
  }

仅应在以下位置使用:

origin: value  //LatLng, String of address or google.maps.Place

然后将{query:..}替换为LatLng对象,错误消失了。 我发现将这个答案放在这里是合理的,因为像我这样的人会复制过去的示例代码来获取时间并信任它,而看不到或不知道这是什么。