首页 > 解决方案 > 角度地理定位 - 显示某个位置,而不是我当前的位置

问题描述

此函数获取我当前的位置并在地图出现时显示它(带有坐标),但我想要的是输入另一个位置(假设我想从Austin, Texas一开始就显示带有坐标的地图,而不是我当前的位置)。

我试图将getCurrentPosition要显示的位置作为参数提供,但我得到了Argument of type 'string' is not assignable to parameter of type 'PositionCallback'.正常的错误,我要输入的位置是 type String。是否有类似 convert StringtoPosition或添加演员表之类的东西,或者我必须对此函数进行什么修改才能获得我想要的东西?

此代码段取自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;
        });
    }
}

先感谢您!

标签: angulargeolocationnavigator

解决方案


我认为您误解了此代码段的工作原理。

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


推荐阅读