-1

I want to make a table of city and coordinate of the city, and then i have a button which gives me my location(with longitude and latitude)

how can i connect them? lets say there is 2 cities one of them is closer to me, and the other one is far, i want it to automatically put me within the one that is closer to me. for example maybe something like this in a code:

<table>
<tr>
<td id="city">Haifa</td>
<td id="coord">32.81841 34.9885</td>
</tr>

<tr>
<td id="city">Tel-Aviv</td>
<td id="coord">32.083333 34.7999968</td>
</tr>

</table>

and then lets say, my location is: 32.777777 34.999999 how can i make it that it will say i am from Haifa? thanks.

idan half
  • 19
  • 8
  • Maybe the reverse Geocoding API help you https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse This can determine where you coordin here is another question which could be from interest for you https://stackoverflow.com/questions/6548504/how-can-i-get-city-name-from-a-latitude-and-longitude-point – Aalexander Jan 13 '21 at 10:22
  • @Alex i dont want to use google api also i can already get the coordinates of my location, i just dont know how to make it fit to the right location from the tables of cities that i can create.. – idan half Jan 13 '21 at 10:28
  • *my location is: 32.777777 34.999999 how can i make it that it will say i am from Haifa?* The purpose of my comment was to answer this question, because reverse Geocoding is the buzzword to get the name of a city from its coordinates – Aalexander Jan 13 '21 at 10:31
  • @Alex i understand, i also looked into it, the problem is that to get an api key from google, i need to put my visa even tho its free untill u reach a certain point, but i dont want to risk it and make an accident and they will charge me. now, what i want is to sort of stimulate a reverse geocoding, by making a table with lists of cities and coordinated, and by having my own coordinate find what is the closest to me and link it together. hope u understand what i mean, and if not lemme know ill try explain myself better – idan half Jan 13 '21 at 10:38
  • Maybe use the Pythagorean Theorem to calculate the slash length to choose the city? – Eritque arcus Jan 13 '21 at 10:40
  • like compare `Math.sqrt(Math.pow(my_x - city1_x, 2)+Math.pow(my_y - city1_y, 2))` and `Math.sqrt(Math.pow(my_x - city2_x, 2)+Math.pow(my_y - city2_y, 2))`, and choose the smaller city – Eritque arcus Jan 13 '21 at 10:43
  • @idanhalf with link it together you mean ordering it by distance? – Aalexander Jan 13 '21 at 13:40
  • @Alex link the closest position to my position and display the location that is closest to me from the list that id'make. – idan half Jan 14 '21 at 09:19

2 Answers2

1

Distance Sorting Solution

  • Calculate the distances I used the algorithm in the answer of jaircazarin-old-account
  • Sort it from the shortest to the longest distance
  • Display it in the table

const pix = 3.141592653589793;
const radius = 6378.16;

function radians(x) {
  return x * pix / 180;
}

function calculateDistance(lat1, long1, lat2, long2) {



  let dlon = radians(long2 - long1);
  let dlat = radians(lat2 - lat1);

  let a = (Math.sin(dlat / 2) * Math.sin(dlat / 2)) + Math.cos(radians(lat1)) * Math.cos(radians(lat2)) * (Math.sin(dlon / 2) * Math.sin(dlon / 2));

  let angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  return angle * radius;
}

let yourLocationLat = 32.777777;
let yourLocationLong = 34.999999;

let places = [{
  "name": "Tel-Aviv",
  "lat": 32.083333,
  "long": 34.7999968
}, {
  "name": "Haifa",
  "lat": 32.81841,
  "long": 34.9885

}, {
  "name": "Berlin",
  "lat": 52.520008,
  "long": 13.404954
},{
  "name": "Alaska",
  "lat": 66.160507,
  "long": -153.369141

},{
  "name": "Mexico",
  "lat": 19.432608,
  "long": -99.133209
},{
  "name": "Jerusalem",
  "lat": 31.771959,
  "long": 35.217018
}


]
let result = []; 
places.forEach((x) => {
  let resultDistance = calculateDistance(yourLocationLat, yourLocationLong, x.lat, x.long)
  let obj = {
  "city": x.name,
  "lat": x.lat,
  "long": x.long,
  "distance": resultDistance
  }
  
  result.push(obj);
})

// shortest first
result.sort((a, b) => {
 return a.distance - b.distance  
})

// now you have in result the data which you can display in your table



document.getElementById("valueInsert").innerHTML = result[0].city;
<div id="valueInsert"></div>
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • hey alex, thanks alot for this answer, i dont know if it works for me, didnt try it but i want to first understand what you did, so my first question is that if u can explain to me the begging part what is it for? ``` const pix = 3.141592653589793; const radius = 6378.16; function radians(x) { return x * pix / 180; } ``` and then if u can explain to me the calculatedistance function +- so i can understand it the dlon and dlat, i guess ill understand after ull explain the radians part and then ill try understand the let a (let is like variable yes?) sorry for being ignorant.. ty – idan half Jan 14 '21 at 09:24
  • @idanhalf the consts are pi as its name and radius is the radius of the earths equatorian. The function radians is just a converter from degrees in radians. let is like var but with local scope in JS. – Aalexander Jan 14 '21 at 09:38
  • oh, okay thanks, ill try put it all together and edit incase i have a quetsion :) thanks alot for the help. – idan half Jan 14 '21 at 09:44
  • @idanhalf you're welcome :) if you think this was helpful mark it as accepted the greyed out button below up-/downvote to show other people that this answer has helped you. – Aalexander Jan 14 '21 at 09:46
0

my solution is using the Pythagorean Theorem to calculate the slash length from your location to the cities, and the city(like compare Math.sqrt(Math.pow(my_x - city1_x, 2)+Math.pow(my_y - city1_y, 2)) and Math.sqrt(Math.pow(my_x - city2_x, 2)+Math.pow(my_y - city2_y, 2))) with smaller length is the answer

            function getLength(ax, ay, bx, by){
               return Math.sqrt(Math.pow(ax  - bx, 2)+Math.pow(ay  - by, 2));
            }
            function chooseCity(){
               let city1 = [32.81841, 34.9885];//Haifa
               let city2 = [32.083333, 34.7999968];//Tel-Aviv
               let mine = [32.777777, 34.999999];//your location
               if(getLength(city1[0], city1[1], mine[0], mine[2]) > getLength(city2[0], city2[1], mine[0], mine[2])){
                   // is the city2
                   console.log("city2")
                }else{
                   // is the city1
                   console.log("city1")
                }
            }
            chooseCity();
Eritque arcus
  • 164
  • 1
  • 11
  • i understand what you did there, but won't it work for 2 cities? and for more there will need to be alot of if else if per city etc.. – idan half Jan 14 '21 at 09:20
  • @idan half uh? do you mean you need to compare more city? you can use the `distance()` function like the another solution, or use a array to calculate one by one. If you want the new version, I can update it. – Eritque arcus Jan 14 '21 at 10:54