Javascript 将地理位置绑定到类的ShowPosition

Javascript 将地理位置绑定到类的ShowPosition,javascript,Javascript,我是JavaScript新手,但我想将地理位置代码封装在一个类中。但是当我这样做的时候。this.showPosition()调用实际上并没有绑定它,如果我使用console log,我会改为使用get窗口。有没有办法把这个绑起来。我知道我可以在ShowPosition中调用getElementById,但那会达到我的目的 function LocationClass() { this.map = null; this.divLocation = null; this.In

我是JavaScript新手,但我想将地理位置代码封装在一个类中。但是当我这样做的时候。
this.showPosition()
调用实际上并没有绑定它,如果我使用console log,我会改为使用get窗口。有没有办法把这个绑起来。我知道我可以在
ShowPosition
中调用
getElementById
,但那会达到我的目的

function LocationClass()
{
     this.map = null;
     this.divLocation = null;

this.Initialize = function()
{
    this.divLocation = document.getElementById("Location");

    this.map = new google.maps.Map(document.getElementById('Map'), 
    {
      center: { lat: -34.397, lng: 150.644 },
      zoom: 10,
      fullscreenControl: false,
      mapTypeControl: false,
      scrollwheel: false,
      zoomControl: false,
      streetViewControl: false
    });

    console.log(this.divLocation);
}

this.UpdateMap = function(aLong, aLat)
{
    this.map.panTo({lat: aLat, lng: aLong});
}

this.GetLocation = function()
{
    if (navigator.geolocation) 
    {
        navigator.geolocation.getCurrentPosition(this.ShowPosition, this.ShowError);
    } 
    else 
    {
        this.divLocation.innerHTML = "Geolocation is not supported by this browser.";
    }
}

this.ShowPosition = function(aPosition)
{
    this.divLocation.innerHTML = "<strong>Latitude:</strong> " + aPosition.coords.latitude +
    "<br><strong>Longitude:</strong> " + aPosition.coords.longitude;
    var geocoder = new google.maps.Geocoder();
    var location = new google.maps.LatLng(aPosition.coords.latitude, aPosition.coords.longitude);       
    geocoder.geocode({ 'latLng': location }, function (results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK)
        {           
            //var add = results[0].formatted_address;
            let city = "";
            let components = results[0].address_components;
            for (var component = 0 ; component < components.length; component++)
            {
                if (components[component].types[0] == "postal_town")
                {
                    city = components[component].long_name;
                }
            }
            this.divLocation.innerHTML += "<br><strong>City:</strong> " + city + "</strong>";
        }
    });

    weatherApp.GetWeatherByCoords(aPosition.coords.latitude, aPosition.coords.longitude);
}

this.ShowError = function(aError)
{
    switch(aError.code) 
    {
        case aError.PERMISSION_DENIED:
        this.divLocation.innerHTML = "User denied the request for Geolocation."
            break;
        case aError.POSITION_UNAVAILABLE:
        this.divLocation.innerHTML = "Location information is unavailable."
            break;
        case aError.TIMEOUT:
        this.divLocation.innerHTML = "The request to get user location timed out."
            break;
        case aError.UNKNOWN_ERROR:
        this.divLocation.innerHTML = "An unknown error occurred."
            break;
    }
}
}
函数位置类()
{
this.map=null;
this.divLocation=null;
this.Initialize=函数()
{
this.divLocation=document.getElementById(“位置”);
this.map=new google.maps.map(document.getElementById('map'),
{
中心:{纬度:-34.397,液化天然气:150.644},
缩放:10,
全屏控制:错误,
mapTypeControl:false,
滚轮:错误,
动物控制:错误,
街景控制:错误
});
console.log(this.divLocation);
}
this.UpdateMap=函数(沿,aLat)
{
panTo({lat:aLat,lng:aLong});
}
this.GetLocation=函数()
{
if(导航器.地理位置)
{
navigator.geolocation.getCurrentPosition(this.ShowPosition,this.ShowError);
} 
其他的
{
this.divLocation.innerHTML=“此浏览器不支持地理位置。”;
}
}
this.ShowPosition=功能(位置)
{
this.divLocation.innerHTML=“纬度:”+aPosition.coords.Latitude+
“
经度:”+aPosition.coords.Longitude; var geocoder=new google.maps.geocoder(); var location=new google.maps.LatLng(aPosition.coords.latitude,aPosition.coords.longitude); geocoder.geocode({'latLng':位置},函数(结果,状态) { if(status==google.maps.GeocoderStatus.OK) { //var add=results[0]。格式化的\u地址; 让城市“”; 让components=results[0]。寻址\u组件; 对于(var组件=0;组件城市:“+City+””; } }); weatherApp.GetWeatherByCoords(aPosition.coords.latitude,aPosition.coords.latitude); } this.shourror=函数(aError) { 开关(错误代码) { 案例A错误许可\u被拒绝: this.divLocation.innerHTML=“用户拒绝了地理定位请求。” 打破 案例错误位置不可用: this.divLocation.innerHTML=“位置信息不可用。” 打破 案例A错误超时: this.divLocation.innerHTML=“获取用户位置的请求超时。” 打破 案例错误未知错误: this.divLocation.innerHTML=“发生未知错误。” 打破 } } }
“this”是javascript中的保留关键字,由其使用范围决定。你不能把任何东西绑在上面

这里有一个问题,有一些很好的资源可以更好地理解“this”关键字:

如果您需要在一个函数/作用域的其他位置传递“this”的值,请将其保存到一个变量中,并使用该变量调用它。

我相信如果使用.bind(),您将覆盖原型。