Coder Social home page Coder Social logo

closest-location's Introduction

Closest Location

This is a tiny and simple script created in response to this question, http://stackoverflow.com/questions/21279559/geolocation-closest-locationlat-long-from-my-position

All credit goes to Andrew for the original code, http://stackoverflow.com/users/622104/andrew-opengeocode

Installation

If you are using jQuery, then simply just add the file to your index.html and you are right to go.

If you are using Angular, then copy the code into a service and make sure to create the closestLocation function on the service's scope like so:

this.closestLocation = closestLocation;

How to use

There's only three things you need to bring to this pool party, a latitude, a longitude and an array of arrays that contains your locations that you want to compare the latitiude and longitiude too. The array of arrays must be structed like below:

var locations = [
  ['Name of Location', latitude, longitude],
  ['Name of Location', latitude, longitude],
  ['Name of Location', latitude, longitude]
]

When you've got all three things. Just pass each variable into the closestLocation function like so:

closestLocation(latitude, longitude, locations);

Obviously, if you are using Angular it would look something more similiar to this:

LocationService.closestLocation(latitude, longitude, locations);

The function will return the array that is closest to the latitude and longitude you passed it.

Say you wanted to get just the name of the location that is closest? Easy.

// Store the value returned from closestLocation into a variable
var close = closestLocation(latitude, longitude, locations);
// Then grab the value at the first index of the array
close[0]

Example

Most people will want to use this code to work out what city etc is closest to a users location.

This can achieved using the Geolocation API in browsers these days.

// get our three parameters ready
var lng,
    lat,
    cities = [
["Sydney",    -33.867487,  151.206990],
["Melbourne", -28.083627,  -80.608109],
["Brisbane",  -27.471011,  153.023449],
["Adelaide",  -34.928621,  138.599959],
["Perth",     -31.953513,  115.857047]
];

// HTML5/W3C Geolocation
if (navigator.geolocation) {
  // get the users current position
  navigator.geolocation.getCurrentPosition(function(position) {
    // store their latitude and longitude into the variables we set up above
    lng = position.coords.latitude;
    lat = position.coords.longitude;
    
    // in this case I just want the name of the city, so I'm storing the returned value into a variable
    var usersCity = closestLocation(lng, lat, cities);
    
    // I want to keep this value for when the user returns to the site or app
    window.localStorage.usersLocation = usersCity[0];

  }, function(error) {
    // logs the error if we can't get the users current position for some reason
    console.log(error);
  });
}

closest-location's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.