首页 > 解决方案 > 无法在 getCurrentPosition 函数上获取简单成功回调的坐标

问题描述

getUserLocation() 和 if 语句一样触发。navigator.geolocation.getCurrentPosition() 函数不会触发。我无法在该声明中记录任何内容。我不知道为什么什么也没发生..

只需登录箭头功能。那里什么也没有显示。

import { Component, OnInit } from '@angular/core';

@Component({
    selector: 'google-map',
    templateUrl: './google-map.component.html',
    styleUrls: ['./google-map.component.css']
})
export class GoogleMapComponent implements OnInit {

  lat: number;

      lng: number;

      constructor() { }

      ngOnInit() {
        this.getUserLocation();

  }

   getUserLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition( position => {
          this.lat = position.coords.latitude;
          this.lng = position.coords.longitude;
      });
    }
  }

}

我需要 lat/lng 的值。

标签: angulargeolocation

解决方案


它要求用户在浏览器提示时同意位置共享。

如果您看到错误“The Geolocation service failed.”,这意味着您可能没有授予浏览器定位您的权限。

检查 api文档


推荐阅读