how to get the point2(destinationPoint) with point1(startPoint) in latlng and distance in meters

StackOverflow https://stackoverflow.com/questions/23512352

  •  16-07-2023
  •  | 
  •  

Question

I have a point1 in latlng format and a distance in meter format. Now my question is

How can I get Point2 by using the point1 and distance?

I found many related questions and just cannot understand their extended discussion.

I just want simple javascript formula or code to demonstrate me how to do the calculation.

Thank you very much.


Update:

The distance contains 2 directions: x and y

The X meters offset from the centre.

The Y meters offset from the centre.

And in here, centre is point1. So

point1 looks like : -34.127895, 140.56842

distance looks like: 3532 meter, 9211 meter


Update 2

enter image description here

I almost there, I found the formula to calculate the 3rd(unknown)side of the triangle. Now I have sin(wantedAngel) = sine(90)/(X*Y). Now I just want to know how to do the un-sin() math operation in Javascript. wantedAngel is bearing.

Was it helpful?

Solution 2

All credits go to @geocodezip :)

JavaScript:

function initialize() {
 var map = new google.maps.Map(document.getElementById('map-canvas'), {
  center: new google.maps.LatLng(-34.127895, 140.56842),
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  var bounds = new google.maps.LatLngBounds();

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
  position: map.getCenter(), 
  map: map
   }); 
    bounds.extend(map.getCenter());
   var south = google.maps.geometry.spherical.computeOffset(marker.getPosition(),3532,180);
    var line1 = new google.maps.Polyline({
        map:map,
        path: [marker.getPosition(),south],
        strokeWeight: 2,
        strokeColor: "#ff0000"
    });
    bounds.extend(south);
  var destination = google.maps.geometry.spherical.computeOffset(south,9211,90);
    bounds.extend(destination);
    var line2 = new google.maps.Polyline({
        map:map,
        path: [south, destination],
        strokeWeight: 2,
        strokeColor: "#ff0000"
    });


    var destMarker = new google.maps.Marker({
      position: destination, 
      map:map
  });    
    map.fitBounds(bounds);
    document.getElementById('info').innerHTML = "distance is "+(google.maps.geometry.spherical.computeDistanceBetween(marker.getPosition(), destination)/1000.0).toFixed(2)+"  km";
    var line3 = new google.maps.Polyline({
        map:map,
        path: [marker.getPosition(), destMarker.getPosition()],
        strokeWeight: 2,
        strokeColor: "#0000FF"
    });
}


google.maps.event.addDomListener(window, 'load', initialize);

Html

<div id="map-canvas"></div>
<div id="info"></div>

CSS

#map-canvas, html, body {
    height: 95%;
    width: 100%;
}

Or you can take a look at :

http://jsfiddle.net/A8yZL/1/

OTHER TIPS

You won't be able to calculate this the easy way. Latitude and longitude are not easily convetable to meters because of the earth not being a flat surface.

Have a look at this table (it would help reading the whole article). You can see that one degree latitude equals 111.32 km. Going 23 degree to the north it equals only 102.47 km until it gets to zero reaching the north pole.

Have a look at the circles getting smaller to the top and the bottom:

Latitude Rings

I think it won't get much easier than the approach provided here: How to calculate the latlng of a point a certain distance away from another?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top