我正在使用 Angular 谷歌地图来设置当前位置和 agm-direction 来设置方向。下面是代码,我遇到了上述错误。
最佳答案
这可能是因为当您调用 getDirections()
时,您的组件的 lat
、lng
没有被填充。 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()
将最多被调用两次..
关于angular - 无效值错误 : in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object; and not an Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52260355/