如何使用此地理位置脚本缓存用户的位置?

时间:2011-09-08 18:20:03

标签: javascript jquery geolocation

如何缓存用户的位置,以便在用户点击后退按钮或多次访问网站时不会反复询问用户的位置?

<!-- Yahoo! GeoIP Service -->
<script src="js/yqlgeo.js"></script>  

<!-- Request users location -->
<script>  
jQuery(window).ready(function(){  
    initiate_geolocation();  
})  

function initiate_geolocation() {  
    if (navigator.geolocation)  
    {  
        navigator.geolocation.getCurrentPosition(handle_geolocation_query, handle_errors);  
    }  
    else  
    {  
        yqlgeo.get('visitor', normalize_yql_response);  
    }  
}  

function handle_errors(error)  
{  
    switch(error.code)  
    {  
        case error.PERMISSION_DENIED: alert("User did not share location.");  
        break;  

        case error.POSITION_UNAVAILABLE: alert("Could not detect your location.");  
        break;  

        case error.TIMEOUT: alert("Retrieving location timed out.");  
        break;  

        default: alert("Unknown Error");  
        break;  
    }  
}  

function normalize_yql_response(response)  
{  
    if (response.error)  
    {  
        var error = { code : 0 };  
        handle_error(error);  
        return;  
    }  

    var position = {  
        coords :  
        {  
            latitude: response.place.centroid.latitude,  
            longitude: response.place.centroid.longitude  
        },  
        address :  
        {  
            city: response.place.locality2.content,  
            region: response.place.admin1.content,  
            country: response.place.country.content  
        }  
    };  

    handle_geolocation_query(position);  
}  

function handle_geolocation_query(position){  
    alert('Lat: ' + position.coords.latitude + ' ' +  
          'Lon: ' + position.coords.latitude);  
    }

/* Display Google Static Map with user's location, custom URL encoded pin injected into link */
function handle_geolocation_query(position)  
{  
    var image_url = "http://maps.google.com/maps/api/staticmap?sensor=false&center=" + position.coords.latitude + "," +  
                    position.coords.longitude + "&zoom=12&size=225x116&style=feature:water|element:geometry|hue:0x336699|saturation:30|lightness:25&style=feature:road.highway|element:labels|visibility:off&style=feature:transit|element:all|visibility:off|" +  
                    position.coords.latitude + ',' + position.coords.longitude;  

    jQuery("#map").remove();  
    jQuery('#welcome_map').append(  
        jQuery(document.createElement("img")).attr("src", image_url).attr('id','map')  
    );  
}    
</script>

2 个答案:

答案 0 :(得分:1)

我个人推荐jQuery webStorage plugin。检查开头的存储位置是否存在。如果没有,请尝试使用navigator.geolocation或使用$.getScript包含yqlgeo.js。 (当然,一旦确定,请保存位置。不要忘记笔记本电脑也可以旅行。)

答案 1 :(得分:1)

这里有一些代码:只需用它来存储lat,很长!

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}