2017-07-06 34 views
0

我做某件事情黑名單與位置服務不需要的位置。這裏是我當前的代碼:我將如何將值與用戶的,然後檢查是否被列入黑名單

我怎樣才能比較值?

主要腳本:

 if (navigator.geolocation) { 
    // Locate position 
    navigator.geolocation.getCurrentPosition(displayPosition, errorFunction 

); 
    } else { 
     alert('Your device location is not approved.'); 
    } 

    // Success callback function 
    function displayPosition(pos) { 
     var mylat = pos.coords.latitude; 
     var mylong = pos.coords.longitude; 
     var thediv = document.getElementById('locationinfo'); 
     alert('Your HWID is compliant of ProtoProt regulations.'); 
    } 
     function errorFunction(pos) { 
     alert('Error: (PROTOPROT_POS_DENIED). We only use your HWID for checking compliance. Enable location to enter.'); 
     } 

VALUES詞典:

blacklisted_areas = { 
    'area 51' : [w, y], // Replace w y with the floats of long and lat 
    'pink unicorn zoo' : [w, y], // same 
    // etc. 
}; 

回答

0

你需要返回一個包含兩個緯度和經度陣列,您displayPosition功能,然後針對鍵比較這陣blacklisted_areas

這是可以做到以下(在ES6):

blacklisted_areas = { 
 
    'area 51': [1, 2], // Replace w y with the floats of long and lat 
 
    'pink unicorn zoo': [1, 2], // same 
 
}; 
 

 
function displayPosition(pos) { 
 
    var mylat = 1; 
 
    var mylong = 2; 
 
    return [mylat, mylong]; 
 
} 
 

 
var blacklisted = false; 
 

 
for (let x of Object.values(blacklisted_areas)) { 
 
    if (displayPosition()[0] === x[0] && displayPosition()[1] === x[1]) { 
 
    blacklisted = true; 
 
    } 
 
} 
 

 
console.log(blacklisted);

在上面的代碼中,變量blacklisted將返回true如果該位置被列入黑名單,並false如果變量未列入黑名單。從這裏,你可以適應西裝。

希望這會有所幫助! :)

+0

太謝謝你了! –

+0

但是我有一些麻煩把劇本在一起,因爲當我測試列入「黑名單」爲真,甚至要求用戶的位置之前,它給人的成功味精。 –

+0

我稍微修改了代碼,現在應該可以正確返回。注意'的console.log(黑名單)'在我的例子不是一個函數或回調裏面,所以會輸出儘快達到的代碼塊。在您使用地理位置時,我假設**您正在輪詢服務,這需要時間來執行。您可能需要從鏈我的例子整個'for'環路輸出回調從這項服務中,所以它實際上得到的位置時,它會檢查它是否被列入黑名單之前;) –

相關問題