0

I am after a very simple distance between two 2D points function for PHP.

For example lat, lng of -50000.00,400.00. This is a flat surface, no need calculate for the earth circumference.

I have tried all sorts of solutions for this and none of them work correctly. I do not know why but when lat or lng is > ~240.00 the result starts going backwards as if the distance is too great to calculate. Even the online calculators fail to calculate a large distance.

Any help would be greatly appreciated.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nathum
  • 3
  • 2
  • I have googled an found following links. These might help you. http://www.geodatasource.com/developers/php http://www.techrecite.com/distance-formula-in-php-calculate-distance-between-two-points-programmatically/ – Manish Shukla Apr 19 '15 at 14:25

1 Answers1

0

Perhaps this is more a mathematical problem.

I think you can have a first distance solution with tranlation of general formula:

https://www.mathsisfun.com/algebra/distance-2-points.html

You have to create a PHP function that make the square the difference for each axis, then sum them up and take the square root.

Alternatively, you can make the follow:

http://www.geodatasource.com/developers/php

Let me know if you need help!

  • Hi thanks for your help, but..... echo distance(200.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles
    "; Returns..... 11839.196960717 Miles AND echo distance(500.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles
    "; 7702.347823328 Miles Obviously the distance is much greater. This is the issue I am having with almost all the functions I have tried with large numbers. The mathsisfun link I have in my browser for a couple of days and would be perfect if I could work out the php math :)
    – Nathum Apr 19 '15 at 23:45
  • I'm not sure if this is correct, but I am getting a much better result. echo(sqrt(($X2 - $X1) + ($Y2 - $Y1))); – Nathum Apr 20 '15 at 00:12
  • I think it's right! It depends on your task. :) – Danilo Costa Apr 20 '15 at 10:12
  • @DaniloCosta This is not an answer, as the distance between 2 coordinates on a 2D plane is not the same as the distance between 2 coordinates that are projected on a sphere (i.e. you might get correct answers at the equator, but as you go towards the poles, your answers will be exaggerated). – Artur Grigio Mar 11 '17 at 18:10