我们如何在angular2打字稿中获得地理位置?

时间:2017-08-10 13:22:37

标签: javascript angular typescript

我试图在angular2上获得地理位置,但到目前为止还不可能。 请检查我尝试的代码。

location = {};
  setPosition(position){
  this.location = position.coords;
  console.log(position.coords);
  }

     ngOnInit(){
        if(window.navigator.geolocation){
          window.navigator.geolocation.getCurrentPosition(this.setPosition.bind(this));
      };
}

2 个答案:

答案 0 :(得分:1)

是:

Rectangle currentPageRectangle = pdfReader.GetPageSizeWithRotation(<PageNumberHere>);
if (currentPageRectangle.Width > currentPageRectangle.Height)
{
    //page is landscape
}
else
{
    //page is portrait
}

这是使用typescript的最小例子:

navigator.geolocation.getCurrentPosition()

答案 1 :(得分:0)

试试这个,这对我有用

 if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(position => {
        this.lat=position.coords.latitude;
        this.long=position.coords.longitude;
        var latlng = { lat: this.lat, lng:this.long };

       let geocoder = new google.maps.Geocoder();
     geocoder.geocode(  {'location':latlng}, (results, status) => {
     if (status == google.maps.GeocoderStatus.OK) {
     let result = results[0];
     let rsltAdrComponent = result.address_components;
     let resultLength = rsltAdrComponent.length;
     if (result != null) {
     console.log(rsltAdrComponent[resultLength-5].short_name)
     } else {
     window.alert('Geocoder failed due to: ' + status);
     }
     }
     });
     });     
   };