Getting the bearing/heading betweet 2 coordinates

Thanks to rjsteward with his answer over at I have now a simple function for getting the bearing between to coordinates:

The function is meant to be used with google maps, so the parameters are google.maps.LatLng objects

function getBearing (from, to) {
            var lat1 = * Math.PI / 180;
            var lon1 = from.lng();

            var lat2 = * Math.PI / 180;
            var lon2 = to.lng();

            var dLon = (lon2 - lon1) * Math.PI / 180;
            var y = Math.sin(dLon) * Math.cos(lat2);
            var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);

            var bearing = Math.atan2(y, x) * 180 / Math.PI;
            if (bearing < 0) {
                bearing = bearing + 360;
            return bearing;

This allows you to easily set the heading in the streetview, between your “streetviewman” and the actual position you are trying to see. So that you are looking to that direction from the street.

map.getStreetView().setPov({ heading: getBearing(streetViewManPosition, myLocationPosition), zoom: 1, pitch: 0 });

Resulting in something like this:

2 Responses to Getting the bearing/heading betweet 2 coordinates

  1. Very useful. Thank you so much!

  2. Josef says:

    I see a lot of interesting content on your blog. You have
    to spend a lot of time writing, i know how to save you a lot of time,
    there is a tool that creates unique, SEO friendly articles in couple of minutes, just search
    in google – k2 unlimited content

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>