-1

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.

Franva
  • 6,565
  • 23
  • 79
  • 144
  • 1
    you can't without knowing direction – paulitto May 07 '14 at 08:18
  • Distance between 1 point to nearest point (6,0-7,0) is 111.2 km see http://www.movable-type.co.uk/scripts/latlong.html – Sajitha Rathnayake May 07 '14 at 08:36
  • Still you didn't specify the detection. X can be + or - and Y too – Sajitha Rathnayake May 07 '14 at 08:55
  • hi @SajithaRathnayake yes, X and Y can be + or -, so the values of X and Y have already indicate whether they are + or -. If X is -, then it would be -3532. Also, I have gone through the article you provided, it's very close but there is one thing different: I do not have bearing in my case. So those formulas cannot be used in my case. Any other ideas? thx – Franva May 07 '14 at 09:01
  • See this http://stackoverflow.com/a/17787472/2345900 – Sajitha Rathnayake May 07 '14 at 09:06
  • hi @SajithaRathnayake thank you very much for your info. I followed the code in that post and am trying to do the reverse way. However, I found that code may have mistakes: lat1 = toRadians(lat1); lat2 = toRadians(lat2); In these 2 statements, they use lat1 and lat2 without even defining them. – Franva May 07 '14 at 09:28
  • check this [thread](http://stackoverflow.com/questions/2187657/calculate-second-point-knowing-the-starting-point-and-distance) – paulitto May 07 '14 at 12:10
  • hi @paulitto please see my update. I followed the same solution and now, I just need to know how to do the un-sin(bearing) to get the bearing. – Franva May 07 '14 at 12:41
  • in mathematics its called arcsin, there is [asin](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asin) method in javascript Math object – paulitto May 07 '14 at 14:51
  • hi @paulitto thank you for your reply. I tried asin: console.log('un-sin = ' + Math.asin(Math.sin(90))); So I was expecting 90 as the result, but it returned 1.1061869541040008. So I don't think it's correct. Please correct me if I was wrong. thank you. – Franva May 07 '14 at 15:13
  • it returns angle in radians (its mentioned by the link I gave), to convert radians to degrees, `degrees = radians * (180/pi)` – paulitto May 07 '14 at 15:35
  • Hi @paulitto I tried: Math.asin(Math.sin(90)) * (180/Math.PI), but the result isn't 90 which is the supposed result. – Franva May 08 '14 at 02:03
  • 1
    http://jsfiddle.net/A8yZL/1/ – geocodezip May 08 '14 at 05:32
  • 1
    @Franva thats because [Math.sin](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin) also accepts radians as a parameter. This will give you 90: `Math.asin(Math.sin(Math.PI*90/180)) * (180/Math.PI)` – paulitto May 08 '14 at 07:17
  • hi @geocodezip, brilliant code!!!! It solves all my problems. THANK YOU! Could you please post your reply as an answer? so I can mark it. – Franva May 08 '14 at 14:05

2 Answers2

1

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?

Community
  • 1
  • 1
loafer
  • 146
  • 1
  • 4
  • Hi loafer, thank you for your reply. it's very close to my question, but only one thing is different: I do not have bearing. I only have a distance in a Point format which contains X and Y(The X meters offset from the centre. The Y meters offset from the centre.). – Franva May 07 '14 at 08:55
  • @Franva with the values you have you could calculate the bearing. Your X and Y informations can be seen as two lines forming a triangle with the real distance. X and Y are aligned with 90 degrees. Now given two sides and the 90 degrees you can calculate all other values of the triangle and use the solution provided in the link. – loafer May 07 '14 at 09:05
  • You are right :) but I've given back almost all math knowledge back to my teacher....could you please write a few lines of code to show me how to calculate the bearing? thanks a lot – Franva May 07 '14 at 09:41
  • I almost there, I found the formula to do the calculation. 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. – Franva May 07 '14 at 10:10
  • Could you provide your code so far? A jsfiddle would be best. – loafer May 07 '14 at 18:28
  • If it helps, you can reverse the sin, cos and tan functions in js by using asin acos and atan. – loafer May 07 '14 at 22:02
  • hi loafer, thank you for your help. I have found the solution. – Franva May 08 '14 at 14:14
  • @Franva could you please mark my answer as the correct one? That would be great. – loafer May 08 '14 at 22:28
  • hi loafer, your answer is good, but geocodezip provided more direct answer to my question, so I have to wait for his post. Thank you again for your help – Franva May 09 '14 at 06:48
0

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/

Franva
  • 6,565
  • 23
  • 79
  • 144