【问题标题】:How would I cache the location of the user with this geolocation script?如何使用此地理定位脚本缓存用户的位置?
【发布时间】:2011-09-08 18:20:02
【问题描述】:

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

<!-- 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>

【问题讨论】:

  • 你的意思是缓存脚本!还是缓存用户的实际位置?!
  • @Arthur Neves 啊,缓存用户的位置
  • 您可以将位置存储在 cookie 中。
  • 没错。我正想说和@Pekka 一样

标签: javascript jquery geolocation


【解决方案1】:

这里有一些代码给你:只要用它来存储纬度,经度!

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);
    }
  }
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 2016-10-14
      • 2020-05-18
      相关资源
      最近更新 更多