angular - 角度地理定位 - 显示某个位置,而不是我当前的位置
问题描述
此函数获取我当前的位置并在地图出现时显示它(带有坐标),但我想要的是输入另一个位置(假设我想从Austin, Texas
一开始就显示带有坐标的地图,而不是我当前的位置)。
我试图将getCurrentPosition
要显示的位置作为参数提供,但我得到了Argument of type 'string' is not assignable to parameter of type 'PositionCallback'.
正常的错误,我要输入的位置是 type String
。是否有类似 convert String
toPosition
或添加演员表之类的东西,或者我必须对此函数进行什么修改才能获得我想要的东西?
此代码段取自How to Make an Autocomplete Address Fields with Angular Google Maps Api。
private setCurrentPosition() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition((position) => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
this.zoom = 12;
});
}
}
先感谢您!
解决方案
我认为您误解了此代码段的工作原理。
该getCurrentPosition
函数不显示某些位置,它是一个提供当前位置并允许您(作为开发人员)对位置(使用函数)做某事的本机 javascript API (position) => {...}
。
有趣的代码是:
<agm-map [latitude]="latitude" [longitude]="longitude" [scrollwheel]="true" [zoom]="zoom" [fullscreenControl]="true" [mapTypeId]="'hybrid'" >
<agm-marker [latitude]="latitude" [longitude]="longitude"></agm-marker>
</agm-map>
这是agm-map
组件的输入参数,因此为了移动到特定位置,您只需this.latitude=...; this.longitude=...;
在您的AppComponent
推荐阅读
- java - 在文本视图中设置文本
- reactjs - 使用 React-Router 时是否可以抽象 Link 声明?
- python - 向数据框添加词性列
- blazor-server-side - 是否可能:使用 Blazor 服务器端作为客户端应用程序
- teamcity - TeamCity 的子版本中未更新相关参数
- c# - 从用户的手机获取 GPS 以验证他们在美国
- javascript - 禁用 create-react-app 的 SKIP_PREFLIGHT_CHECK 有缺点吗?
- django-rest-framework - 我们如何处理密码更改和忘记密码以及令牌刷新?
- jenkins - 如何触发另一个需要 $BRANCH 变量的 Jenkins 管道?
- aframe - 大家好,我正在尝试根据 URL 参数动态更改 a-text 值