Search code examples
angulargeolocationnavigator

Angular geolocation - display a certain location, not my current position


This function gets my current position and displays it (with it's coords) when the map shows up, but want I want is to input another location (let's say I want to display the map with it's coords from Austin, Texas at the start, not my current position).

I tried to give as argument to getCurrentPosition the location that I want to display, but I get the error Argument of type 'string' is not assignable to parameter of type 'PositionCallback'. which is normal, the location I want to input is type String. Is there something like convert String to Position, or add a cast, or what do I have to modify to this function to obtain what I want?

This snippet is taken from here 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;
        });
    }
}

Thank you in advance!


Solution

  • I think you misunderstood how this snippet works.

    The getCurrentPosition function does not display some location, it's a native javascript API that provides the current location and allows you (as a developer) to do something with the position (with the function (position) => {...}).

    The interesting code is:

    <agm-map [latitude]="latitude" [longitude]="longitude" [scrollwheel]="true" [zoom]="zoom" [fullscreenControl]="true"  [mapTypeId]="'hybrid'" >
       <agm-marker [latitude]="latitude" [longitude]="longitude"></agm-marker>
    </agm-map>
    

    Here are the input parameters of the agm-map component, so in order to move to a specific location you just have to set another latitude and longitude using this.latitude=...; this.longitude=...; in your AppComponent