-1

EDIT: I have self-answered it below.

I have made a question on this before, however I could not properly set it up and the whole thing was broken. Below is the geolocation script. I would like to implement the blacklisting of certain areas (within a 500 foot radius if possible). Please explain to me how and where to make changes, as I am a beginner in JavaScript. thank you. (NOTE: No list or dictionary is on the page as it was part of my problem.)

current code:

if (navigator.geolocation) {
    // Locate position
    navigator.geolocation.getCurrentPosition(displayPosition, errorFunction);
} else {
    alert('It seems like Geolocation, which is required for this page, is not enabled in your browser. Please use a browser which supports it.');
}

// Success callback function
function displayPosition(pos) {
    var mylat = pos.coords.latitude;
    var mylong = pos.coords.longitude;
    var thediv = document.getElementById('locationinfo');
    thediv.innerHTML = '<p>Your longitude is :' +
        mylong + ' and your latitide is ' + mylat + '</p>';

// Error callback function
function errorFunction(pos) {
    alert('Error!');
}
  • 3
    Possible duplicate of [Determine if a Longitude & Latitude Co-ordinate is Inside a Radius in Miles and Kilometers](https://stackoverflow.com/questions/23115375/determine-if-a-longitude-latitude-co-ordinate-is-inside-a-radius-in-miles-and) – yuriy636 Jul 07 '17 at 22:42
  • 1
    This might also help: https://stackoverflow.com/questions/24680247/check-if-a-latitude-and-longitude-is-within-a-circle-google-maps – chrki Jul 07 '17 at 22:44
  • Thanks both of you – meme master Jul 07 '17 at 22:48
  • However, the posts do not show how to implement these into my current system. If you know a way to do this please tell me – meme master Jul 07 '17 at 23:01
  • blacklist? in what way? you're currently displaying long/lat values and nothing else – Jaromanda X Jul 07 '17 at 23:02
  • I know that.... That is for my testing purposes, so that should be ignored. – meme master Jul 07 '17 at 23:04
  • And similar to the posts linked above I want to add a radius. – meme master Jul 07 '17 at 23:05
  • so, you have no relevant code to show? great. So how do you intend on blacklisting certain locations? You know what you mean, but we have no idea how you intend on blacklisting – Jaromanda X Jul 07 '17 at 23:05
  • add a radius to WHAT? the code you've shown is irrelevant (according to you) so what are you adding a radius to? – Jaromanda X Jul 07 '17 at 23:06
  • And as I said there is NO blacklisting code in it because I had to scrape it – meme master Jul 07 '17 at 23:06
  • you still haven't explained what your problem is ... you want "blacklisting code" - that doesn't explain a single thing – Jaromanda X Jul 07 '17 at 23:07
  • Listen. I need to add a dicitionary or list in which i will list the lat and long blacklisted. Its NOT implemented either. It was part of the problem – meme master Jul 07 '17 at 23:07
  • 2
    ahhh, now we're getting somewhere ... so you already have a list of blacklisted co-ordinates, you just need to find the ones that are within 500ft of a particular location - am I getting warmer? – Jaromanda X Jul 07 '17 at 23:09
  • Yes warmer than warm milk. I lastly need a secure way to store the list of blacklisted coords (I use Comodo SSL if that means anything) in addition to how to get them in the radius thing – meme master Jul 07 '17 at 23:10
  • I did, it isnt stored on site because it caused security issues, I have a plain text list of them in a text file on my PC xD – meme master Jul 07 '17 at 23:13
  • and if you are just going to ask questions and downvote me as soon as you see something you dont like i might as well just delete the post because no one is going to see it now – meme master Jul 07 '17 at 23:26
  • don't **ass**ume I downvoted - and I ask questions because as it stands your question is had to understand - no mention that you have a list of blacklisted locations in it at all – Jaromanda X Jul 07 '17 at 23:27
  • Shouldn't this rather be on `Math` somewhere :) What kind of coordinates did you save in your text file? Could you give some sample coordinates? – Icepickle Jul 08 '17 at 11:48
  • I am using my home's coords for the test – meme master Jul 08 '17 at 16:07
  • I wont be adding any others until its finished – meme master Jul 08 '17 at 16:08

2 Answers2

1

given a blacklist structure as follows (as you refuse to share anything about the blacklist data you have)

var blacklistedCoordinates = [
    { longitude: 0, latitude: 0},
    { longitude: 1, latitude: 10},
    { longitude: 2, latitude: 20},
    { longitude: 3, latitude: 30},
    { longitude: 4, latitude: 40}
];

your code, modified

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(displayPosition, errorFunction);
} else {
    alert('It seems like Geolocation, which is required for this page, is not enabled in your browser. Please use a browser which supports it.');
}

// Success callback function
function displayPosition(pos) {   
    var mylat = pos.coords.latitude;
    var mylong = pos.coords.longitude;
    var thediv = document.getElementById('locationinfo');
    thediv.innerHTML = '<p>Your longitude is :' + mylong + ' and your latitide is ' + mylat + '</p>';
    // additional code to output blacklist locations within 500ft
    blacklistedCoordinates
    .filter(black => calcCrow(black, pos.coords) < 500)
    .forEach(black => { // output blacklisted locations
        thediv.innerHTML = '<p>Blacklisted longitude is :' + black.longitude + ' and latitide is ' + black.latitude + '</p>';
    });
}
// Error callback function
function errorFunction(pos) {
    alert('Error!');
}

code from https://stackoverflow.com/a/28673693/5053002 - modified to return feet

function calcCrow(coords1, coords2) {
    const toRad = value => value * Math.PI / 180;
    const R = 20.90223164; // work in feet
    const dLat = toRad(coords2.latitude - coords1.latitude);
    const dLon = toRad(coords2.longitude - coords1.longitude);
    const lat1 = toRad(coords1.latitude);
    const lat2 = toRad(coords2.latitude);

    const a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    const d = R * c;
    return d;
}
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • thank you, is there a way to redirect if a user is blacklisted? – meme master Jul 07 '17 at 23:40
  • I can't see how that relates to the question, search how to redirect – Jaromanda X Jul 07 '17 at 23:42
  • I know how to redirect, but how can i do an if() for it is what I mean – meme master Jul 07 '17 at 23:43
  • `if (blacklistedCoordinates.some(black => calcCrow(black, pos.coords) < 500)) { /* blacklisted, redirect */` – Jaromanda X Jul 07 '17 at 23:44
  • hold on, its giving not giving me any warning? has this been tested, and if so maybe im assembeling it wrong (even though simplest instructions) – meme master Jul 08 '17 at 00:01
  • `has this been tested` - yes, I'm currently within 500ft of 0N 0E and it works – Jaromanda X Jul 08 '17 at 00:02
  • you know you could just copy your values and change it instead of moving to the center of the planet, because though all values are defined as far as I can see, I do not get a warning even though I am pretty sure I live within 500 feet of my house. maybe im dreaming my location not to sure i better wake up though – meme master Jul 08 '17 at 00:05
  • seems like the ANSWER in the other question doesn't result in **metres** as I assumed – Jaromanda X Jul 08 '17 at 00:15
  • okay, no idea what that means but i assume that means it is somehow fixable – meme master Jul 08 '17 at 00:19
  • changed the constant `R` so the result `d` is in feet - also fixed a typo in the code – Jaromanda X Jul 08 '17 at 00:21
  • woah thats rad it works thank you sorry for a being a dick earlier i was frustrated because i could not understand how to assemble anything – meme master Jul 08 '17 at 00:31
  • if you can help, there is a problem for me where it says im blacklisted though no data other than 0 is stored? I might have put the if() in the wrong area, please tell me the issue. – meme master Jul 08 '17 at 05:39
  • @Kakumei don't be a [help vampire](https://meta.stackexchange.com/questions/19665/the-help-vampire-problem), ask a new question if something is not clear – Icepickle Jul 09 '17 at 10:59
  • yo thats just what I did and I got a link to mozilla which means i will starting all over so i dont know what you want with me dude – meme master Jul 09 '17 at 19:20
0

Thanks to Jaramoda for is awenser, it contributed to the final solution. First off, the code for calculated the radial coordinates was broken, I decided to replace it with the original and make changes so that it would work with my current code:

function calcCrow(coords1, coords2)
{
  // var R = 6.371; // km
  var R = 6371000;
  var dLat = toRad(coords2.latitude - coords1.latitude);
  var dLon = toRad(coords2.longitude - coords1.longitude);
  var lat1 = toRad(coords1.latitude);
  var lat2 = toRad(coords2.latitude);

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c;
  return d;
}

I attached it to the same area, and specified it for 250meters (270 yards?) I will of course need to tinker with the values for more ideal distances.

Next, I tested it without my coordinates in the dictionary, and it did not return anything (a good thing, this was the problem before). And then I entered coordinates and it worked just fine.

This took almost a day to figure out the source of the issue for me, though it seems quite obvious now xD