Search code examples
phpgeolocationgeocoding

How to move x distance in any direction from geo coordinates?


I'm using Google Places API to get a list of establishment near a geo location, and I'm trying to figure out how to move 1km north and get geo coordinates there and then do another Google API call.

Ideally I'd like to be able to go into any direction for any distance and get the geo coordinates. Where do i start? If this is not possible then please let me know.

I'm not getting anything. Please have a look. How exactly do i use the direction. How do i go north?

$lat = "40.712714";
$lon = "-74.005997";
$direction = "80";
$length="2000";

 move($lon, $lat, $direction, $length);


function move($cx,$cy,$direction,$length){
    $x=$cx*cos($direction*M_PI/180)+$length;
    $y=$cy*sin($direction*M_PI/180)+$length;
    return array($x,$y);
    echo $x;
    echo $y;
    }

EDIT I just tried changing the coordinates number manually and it seems to do what I want. For example I take these coordinates

40.717828,-73.998672

and changed the 3rd decimal number. That moved my location north.

40.710828,-73.998672


Solution

  • I have conducted a little investigation. This is the corrected code for this function:

    function move($ylat,$xlng,$direction_degree,$length_degree){
        $x=$xlng+$length_degree*cos($direction_degree*M_PI/180);
        $y=$ylat+$length_degree*sin($direction_degree*M_PI/180);
        return array($x,$y);
    }
    
    var_dump(move(52.1,11.2,0,1)); //North 1 degree.
    

    However, there are a few minor problems to fix before you can use it as you would like.

    • This function has the distance in degrees, so you need to convert metres to degrees first. Beware that is varies along the latitude.
    • The direction is in degrees, 0 is north, 90 east, 180 south, 270 west.
    • I recommend you orginate from degrees then do metre-conversion lastly. By then you just do distance between old and new coordinates.