Как установить «широту» и «долготу» для текущего местоположения в «AgmCoreModule» - PullRequest
0 голосов
/ 27 апреля 2018

В моем приложении я использую AgmCoreModule для показа google map, проблема в том, что я хотел бы установить latitude и longitude, найдя текущее местоположение пользователя (страну), как этого добиться?

вот мой текущий жесткий код:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { StorageService } from '../../shared/service/storage.service';
import { BehaviorSubject } from 'rxjs';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

    latitude = 51.678418; //instead find current country and latitude
    longitude = 7.809007; //instead find current country and longitude 

}

HTML:

<agm-map [latitude]="latitude" [longitude]="longitude"></agm-map>

отлично работает. Но я хотел бы показать пользователям текущее местоположение страны по центру загрузки страницы. кто-нибудь мне помочь?

Ответы [ 2 ]

0 голосов
/ 16 января 2019

@ 3gwebtrain улучшает свой код.

if(navigator){
navigator.geolocation.getCurrentPosition( pos => {
this.latitude = pos.coords.latitude;
this.longitude = pos.coords.longitude;
})
}
0 голосов
/ 27 апреля 2018

Это работает для меня:

if(navigator){
            navigator.geolocation.getCurrentPosition( pos => {

                this.latitude = pos.coords.latitude;
                this.longitude = pos.coords.longitude;
                this.server.getCountry(this.latitude, this.longitude ).subscribe(res => {
                    console.log( res );
                    this.setCountry(res);
                });
            })
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...