0

im trying to see if this

 Math.sqrt(
           Math.pow((position.coords.latitude -45),2) + 
           Math.pow((position.coords.longitude-75),2)
          )*79; 

Matches this:

 Distance to store (km) = Square Root (
                                       (Current Latitude – 45)^2 + 
                                       (Current Longitude ‐75)^2
                                      ) *79

Right now im getting 11,XXX KM which is way to much, but I don't see any mistakes.

I also tried doing it like this:

var x = Math.pow((position.coords.latitude-45),2);
    var y = Math.pow((position.coords.longitude-75),2);
    var z = Math.sqrt(x+y);
    var zz = z*79;

but it gave me the same answer.

vdua
  • 1,281
  • 1
  • 14
  • 24
Batman
  • 5,563
  • 18
  • 79
  • 155
  • 1
    And what should be the correct value? – VisioN Mar 27 '13 at 10:12
  • 2
    What values do you use ? By the way, prefer `x*x` over `Math.pow(x,2)`, it will be faster. – Denys Séguret Mar 27 '13 at 10:13
  • 1
    I don't think you should move this particular question, but just as a general bit of trivia; did you know there's a stackexchange site for mathematics? http://math.stackexchange.com/ – totallyNotLizards Mar 27 '13 at 10:15
  • 1
    Provide us values of latitude and longitude – 999k Mar 27 '13 at 10:15
  • 1
    @jammypeach this hardly looks like mathematics. The problem is probably in the programming field. – Denys Séguret Mar 27 '13 at 10:15
  • 1
    @dystroy yes. as I said, I'm not suggesting he move it. – totallyNotLizards Mar 27 '13 at 10:17
  • 1
    @dystroy The more I think, the more I understand that the problem is in formula, since the code *absolutely* corresponds to presented maths. – VisioN Mar 27 '13 at 10:19
  • 1
    Both your equations are sematically equivalent. The problem lies within your coordinates. – Christoph Mar 27 '13 at 10:20
  • 2
    [Lattitude and longitude](http://en.wikipedia.org/wiki/Geographic_coordinate) are not in KMs but in degrees, they are angles. Therefore your way of calculating the distance in not going to work. See http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points for a proper way of calculating the distance. – Veger Mar 27 '13 at 10:20
  • 2
    A possible problem is that this formula can only be valid for very very short distances. Better formulas are available [here](http://www.movable-type.co.uk/scripts/latlong.html). – Denys Séguret Mar 27 '13 at 10:20
  • 1
    @VisionN - I'm not sure, i'm trying to get the distance between my house and should. Google maps puts it at 6.9km (bus drive) my result is 11,908 so I just know something is wrong or the equation my prof gave us is wrong. I just wasn't sure if I was converting it to Javascript correctly – Batman Mar 27 '13 at 10:21
  • 1
    @Veger it looks about right as an approximation for very short distances. – Denys Séguret Mar 27 '13 at 10:22
  • @dystroy, wouldn't x*x be multiplication and not to the power of 2 – Batman Mar 27 '13 at 10:24
  • Is your school located at (45,75) ? If so what's the position of your house ? – Denys Séguret Mar 27 '13 at 10:24
  • Well, you probably should have +75, not ‐75 in your equation, then. – Denys Séguret Mar 27 '13 at 10:29
  • Oh wow, so it's a prof mistake then. – Batman Mar 27 '13 at 10:30
  • Just to be sure, are you near Ottawa ? If so, yes, there was a minor typo in your prof's formula. But you should have spotted it by understanding the logic behind. – Denys Séguret Mar 27 '13 at 10:31
  • @AJ, all I have is the lat and long plus the coordinates the prof gave us. But it seems like he gave us +75 when he should have given us -75. – Batman Mar 27 '13 at 10:32
  • @dystroy yes im in ottawa ontario. I live a good 15minutes from the school by car so the distance should be pretty short. Im testing again with the fix to see if i get a more reasonable answer – Batman Mar 27 '13 at 10:33
  • You'll get an answer as precise as the lat and long of your school is. – Denys Séguret Mar 27 '13 at 10:34
  • the prof gave us 45 and I assume he meant -75. Im now getting 65km. Which is way better than 11,908 :/ – Batman Mar 27 '13 at 10:35
  • You probably want to remove/mask your coordinates if you are concerned about privacy now as this problem is solved. – Christoph Mar 27 '13 at 10:45
  • haha good point. Thanks. – Batman Mar 27 '13 at 19:44

1 Answers1

0

This formula is valid for short distances to the point of latitude 45 and longitude 75.

What you want, as you're not located in Kazakstan, is the distance to (45, -75), that is

Math.sqrt(Math.pow((lat-45),2)+Math.pow((lon+75),2))*79; 
                                            ^

Note that it's only as precise as your coordinates are precise and that for bigger distances you should grab a better formula.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Im going to try one of the better formulas tomorrow since 65km is still a pretty high value but thanks for the answer. – Batman Mar 27 '13 at 10:41
  • 1
    The problem is probably more in the precision of your school's coordinates. Locate it on Google Maps to get them. – Denys Séguret Mar 27 '13 at 10:50