JS Geolocator
How do I make an js geolocator?
Uses the visitor location and the Haversine formula to determine which out of a list of four locations is the closest.. What is a js geolocator? How do you make a js geolocator? This script and codes were developed by Joshua P. Larson on 23 July 2022, Saturday.
JS Geolocator - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>JS Geolocator</title> <link rel='stylesheet prefetch' href='https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="jumbotron"> <h1>JS Geolocator</h1> <p> Determine which item in an array is closest to your current location using the Haversine function. </p> </div> </div> </div> <div class="row"> <div class="col-sm-6"> <h2>Locations</h2> <ul id="locations" class="list-group"></ul> </div> <div class="col-sm-6"> <h2>Closest to You</h2> <p id="closest" class="alert alert-success"></p> </div> </div>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
JS Geolocator - Script Codes CSS Codes
html, body { width: 100%; height: 100%; padding-top: 1em;
}
.jumbotron { background: radial-gradient(400px 200px, #eeeeee, #ccddee);
}
JS Geolocator - Script Codes JS Codes
$(document).ready(function() { $.each(locations, function(i, el) { $('<li />').html(el.name).addClass('list-group-item').appendTo('#locations'); });
});
var locations = [{ name : 'New York City', lat : 40.714353, lng : -74.005973
},{ name : 'Sydney', lat : -33.867487, lng : 151.206990
},{ name : 'Los Angeles', lat : 34.052234, lng : -118.243685
},{ name : 'Houston', lat : 29.760193, lng : -95.369390
}];
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(calculate);
}
function toRad(Value) { /** Converts numeric degrees to radians */ return Value * Math.PI / 180;
}
function haversine(lat1, lat2, lng1, lng2) { rad = 6372.8; // for km Use 3961 for miles deltaLat = toRad(lat2-lat1); deltaLng = toRad(lng2-lng1); lat1 = toRad(lat1); lat2 = toRad(lat2); a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) + Math.sin(deltaLng/2) * Math.sin(deltaLng/2) * Math.cos(lat1) * Math.cos(lat2); c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); return rad * c;
}
function calculate(position) { var lat1 = position.coords.latitude; var lng1 = position.coords.longitude; var result = {}, d; for (var i = 0; i < locations.length; i++) { d = haversine( lat1, locations[i].lat, lng1, locations[i].lng); if ((result.distance && d < result.distance) || !result.distance){ result.distance = d; result.location = locations[i]; } } console.log("Result: " + result.location.name); $('#closest').html(result.location.name);
}
Developer | Joshua P. Larson |
Username | jplhomer |
Uploaded | July 23, 2022 |
Rating | 3 |
Size | 2,653 Kb |
Views | 40,480 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Verge-Style Homepage Tiles | 5,147 Kb |
Fixed Nav HoverIntent Example | 4,444 Kb |
Responsive HTML Timeline | 3,137 Kb |
Webkit Text Masking | 1,975 Kb |
Populate Native HTML Datalist with AJAX | 2,763 Kb |
Animated Donut Chart | 3,808 Kb |
Seamless Scrolling Background | 2,535 Kb |
CSS-Only Art Direction | 4,086 Kb |
Hey React | 3,959 Kb |
Lightweight Quiz Widget | 4,596 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Scoreboard.js basic usage | Tbleckert | 1,733 Kb |
Easing | GreenSock | 2,043 Kb |
GrcJS | Vino6 | 2,047 Kb |
CardMove | Thompsonemerson | 3,699 Kb |
CSS3 Media Queries demo | Machal | 1,824 Kb |
Css Rotating 3d cubes different speed | Dghez | 2,364 Kb |
Scarlett Johansson Tribute Page | Diomed | 3,233 Kb |
Animating characters with jQuery | 042 | 2,776 Kb |
Working around OS X Dynamic Scrollbars | Jrjenk | 2,279 Kb |
Cant get enough icecream in pure css3 | Melawire | 4,322 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!