2013-04-10 85 views
0

我發現這個偉大的腳本,根據他們的經度和緯度找到用戶的郵政編碼。通過閱讀代碼,我發現必須有一個狀態ID來使信息變得可見。我的問題是,是否可以製作一個放入狀態ID的信息的cookie。因此,它會根據個人信息提供一個郵政編碼,我希望爲該郵政編碼製作一個cookie,以便在我的網站上進一步使用它。它使用地理位置並將信息插入到谷歌並獲得一個郵政編碼。因此,如果可能的話,我想捕獲該郵政編碼並創建一個cookie。任何幫助將不勝感激。謝謝。從ID創建Cookie?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script> 
</head> 

<body> 
<p>&nbsp;</p> 
<center> 
<p id="status"></p> 
<script> 
$(function(){ 
var GETZIP = { 
    getLocation: function(){ 
    $('#status').text('Searching...'); 
    if(navigator.geolocation){ 
     navigator.geolocation.getCurrentPosition(GETZIP.getZipCode, GETZIP.error,  {timeout: 7000});//cache it for 10 minutes 
    }else{ 
     GETZIP.error('Geo location not supported'); 
    } 
    }, 
    index: 0, 
    error: function(msg) { 
    if(msg.code){ 
     //this is a geolocation error 
     switch(msg.code){ 
     case 1: 
      $("#status").text('Permission Denied').fadeOut().fadeIn(); 
      break; 
     case 2: 
      $("#status").text('Position Unavailable').fadeOut().fadeIn(); 
      break; 
     case 3: 
      GETZIP.index++; 
      $("#status").text('Timeout... Trying again (' + GETZIP.index + ')').fadeOut().fadeIn(); 
      navigator.geolocation.getCurrentPosition(GETZIP.getZipCode, GETZIP.error, {timeout: 7000}); 
      break; 
     default: 
      //nothing 
     } 
    }else{ 
     //this is a text error 
     $('#error').text(msg).addClass('failed'); 
    } 

    }, 

    getZipCode: function(position){ 
    var position = position.coords.latitude + "," + position.coords.longitude; 
    $.getJSON('proxy.php',{ 
     path : "http://maps.google.com/maps/api/geocode/json?latlng="+position+"&sensor=false", 
     type: "application/json" 
    }, function(json){ 
     //Find the zip code of the first result 
     if(!(json.status == "OK")){ 
      GETZIP.error('Zip Code not Found'); 
      return; 
     } 
     var found = false; 
     $(json.results[0].address_components).each(function(i, el){ 
      if($.inArray("postal_code", el.types) > -1){ 
       $("#status").text(Your zip code: + el.short_name); 
       found = true; 
       return; 
      } 
     }); 
     if(!found){ 
      GETZIP.error('Zip Code not Found'); 
     } 
    }); 
    } 
} 
GETZIP.getLocation(); 
}); 
</script> 
+0

您的問題只是「我如何將cookie存儲在cookie中?」。還是有更多的呢? – jfriend00 2013-04-10 04:13:48

+0

不是。它如何捕獲郵政編碼並將其存儲在cookie中。郵政編碼似乎被定義爲「el.short_name」,我無法找到將其妥善存儲爲cookie的方法。 – 2013-04-10 04:16:36

+0

爲什麼你不能把'el.short_name'放入cookie中?你嘗試了什麼?你有什麼問題? – jfriend00 2013-04-10 04:18:06

回答

0

假設你的代碼正確檢索el.short_name郵政編碼,你可以存儲的價值,它的檢索之後:

下面是創建一個cookie功能:

function createCookie(name,value,days) { 
    var expires = "", date; 
    if (days) { 
     date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     expires = "; expires=" + date.toGMTString(); 
    } 
    document.cookie = name+"="+value+expires+"; path=/"; 
} 

這裏的getZipCode的修改版本,它在檢索郵政編碼後立即寫入Cookie:

getZipCode: function(position){ 
    var position = position.coords.latitude + "," + position.coords.longitude; 
    $.getJSON('proxy.php',{ 
     path : "http://maps.google.com/maps/api/geocode/json?latlng="+position+"&sensor=false", 
     type: "application/json" 
    }, function(json){ 
     //Find the zip code of the first result 
     if(!(json.status == "OK")){ 
      GETZIP.error('Zip Code not Found'); 
      return; 
     } 
     var found = false; 
     $(json.results[0].address_components).each(function(i, el){ 
      if($.inArray("postal_code", el.types) > -1){ 
       $("#status").text(Your zip code: + el.short_name); 
       createCookie("zipcode", el.short_name, 365); 
       found = true; 
       return; 
      } 
     }); 
     if(!found){ 
      GETZIP.error('Zip Code not Found'); 
     } 
    }); 
    } 
+0

我想感謝您。這工作完美。 – 2013-04-10 04:37:20