none
how to get the center point between two lat and long points. RRS feed

  • Question

  • Hi all, can any one help me to get the center point between two lat and long. Im drawing a polyline between two cities i want to display a image exactly in the middle of the line. please can any one suggest how to accomplish this task in silverlight bing map.

    thanks in advance.


    raghu
    • Moved by Ricky_Brundritt Friday, March 9, 2012 3:26 PM (From:Bing Maps: Map Control and Web services Development)
    Monday, January 31, 2011 5:43 AM

Answers

All replies

  • Do you want the point that lies in the middle of those two cities on the surface of the Earth, or do you want the point that lies in the middle of the polyline drawn between those two cities on the map?

    In other words, do you want Point A or B?


    Beginning Spatial with SQL Server http://www.apress.com/book/view/1430218290
    Monday, January 31, 2011 1:44 PM
    Moderator
  • Hi Tanoshimi,

    i found the way how to find the mid point that lies in the middle of the two cities :

    i did some thing like this :

    var newlat = (lat1 + lat2 )/2;

    var newlong = (lang1 + lang2)/2 ;

    please correct me if im wrong and also could you please tell me how to accomplish point 'B'.

    Thanks in advance.


    raghu
    Tuesday, February 1, 2011 5:53 AM
  • Take a look at this article: http://rbrundritt.wordpress.com/2008/10/14/calculating-the-midpoint-of-a-line-segment/

     


    Windows Live Developer MVP - http://rbrundritt.spaces.live.com | http://inknowledge.co.uk
    Tuesday, February 1, 2011 12:02 PM
  • Hi Richard,

    Thanks for your valuable reply...

    i went through the link you have suggested. and im doing like this in my code :

    --------------------------------------------

    Location newlocation = midpoint(latpush, longpush);
    objPushpin.Location = newlocation;

    -----------------------------------------------

            private Location midpoint(Location latlong1, Location latlong2)
            {
                double arcLength = haversineDistance(latlong1, latlong2);
                double brng = calculateBearing(latlong1, latlong2);
                return calculateCoord(latlong1, brng, arcLength / 2);
            }


            double earthRadius = 6367;
            private double haversineDistance(Location latlong1, Location latlong2)
            {
                var lat1 = DegtoRad(latlong1.Latitude);
                var lon1 = DegtoRad(latlong1.Longitude);
                var lat2 = DegtoRad(latlong2.Latitude);
                var lon2 = DegtoRad(latlong2.Longitude);
                var dLat = lat2 - lat1;
                var dLon = lon2 - lon1;
                var cordLength = Math.Pow(Math.Sin(dLat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dLon / 2), 2);
                var centralAngle = 2 * Math.Atan2(Math.Sqrt(cordLength), Math.Sqrt(1 - cordLength));
                return earthRadius * centralAngle;
            }


            private double DegtoRad(double x)
            {
                return x * Math.PI / 180;
            }

            private double RadtoDeg(double x)
            {
                return x * 180 / Math.PI;
            }


            private double calculateBearing(Location latlong1,Location latlong2)
            {
                var lat1 = DegtoRad(latlong1.Latitude);
                var lon1 = latlong1.Longitude;  
                var lat2 = DegtoRad(latlong2.Latitude);  
                var lon2 = latlong2.Longitude;  
                var dLon = DegtoRad(lon2-lon1);  
                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 brng = (RadtoDeg(Math.Atan2(y, x))+360)%360;  
                return brng;
            }

            private Location calculateCoord(Location origin, double brng, double arcLength)
            {
                var lat1 = DegtoRad(origin.Latitude);
                var lon1 = DegtoRad(origin.Longitude);
                var centralAngle = arcLength / earthRadius;
                var lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(centralAngle) + Math.Cos(lat1) * Math.Sin(centralAngle) * Math.Cos(DegtoRad(brng)));

                var lon2 = lon1 + Math.Atan2(Math.Sin(DegtoRad(brng)) * Math.Sin(centralAngle) * Math.Cos(lat1), Math.Cos(centralAngle) - Math.Sin(lat1) * Math.Sin(lat2));
                return new Location(RadtoDeg(lat2), RadtoDeg(lon2));

            }

    But its not display the pushpin in the appropriate where i wanted it to be displayed. Can any one please provide me some working code or a example how to accomplish Point 'B' or Point 'A' discussed in my earlier post.

    Thanks in advance.

     


    raghu
    Wednesday, February 2, 2011 2:32 AM
  • Hi tanoshimi,

    Could you please post a solution for Point 'B'. im trying to add a custom pushpin exactly between two cities. i tried what richard has posted but not able to get the approriate solution.

    Thanks in advance.


    raghu
    Thursday, February 3, 2011 2:59 AM
  • Take a look at this article to find the math you are looking for: http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/
    Windows Live Developer MVP - http://rbrundritt.spaces.live.com | http://inknowledge.co.uk
    • Marked as answer by mailtoraghu Thursday, February 17, 2011 7:20 AM
    Monday, February 14, 2011 4:38 PM