我想在变量中获取位置,并希望使用此变量来应用条件

时间:2015-09-08 14:22:23

标签: javascript geolocation

我有两页。一个是登录页面,而在其他页面中,我使用Geolocation查找我的位置。但是在登录页面中我想从位置文件中获取该变量并对该变量应用条件.....

    <script>
    function displayLocation(latitude,longitude){
    var request = new XMLHttpRequest();

    var method = 'GET';
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?           latlng='+latitude+','+longitude+'&sensor=true';
    var async = true;

    request.open(method, url, async);
    request.onreadystatechange = function(){
        if(request.readyState == 4 && request.status == 200){
            var data = JSON.parse(request.responseText);
            var address = data.results[1];
            var check = address.formatted_address;
            document.write(check);


        }
    };
    request.send();
};

var successCallback = function(position){
    var x = position.coords.latitude;
    var y = position.coords.longitude;
    displayLocation(x,y);
};

var errorCallback = function(error){
    var errorMessage = 'Unknown error';
    switch(error.code) {
        case 1:
            errorMessage = 'Permission denied';
            break;
        case 2:
            errorMessage = 'Position unavailable';
            break;
        case 3:
            errorMessage = 'Timeout';
            break;
    }
    document.write(errorMessage);
};

var options = {
    enableHighAccuracy: true,
    timeout: 1000,
    maximumAge: 0
};

navigator.geolocation.getCurrentPosition(successCallback,errorCallback,选项);

当我在我的登录文件中调用此文件时,它只显示位置但登录页面内容消失....请帮助我

0 个答案:

没有答案