Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
115
Centering a map on a longitude, latitude point
posted

I know this should be simple, but I can't seem to get it to work. I have a simple map that is an openStreetsTileSource. I'd like to center the map on a particular location based on the longitude and latitude point. I have written code that I thought would work, but it doesn't:

 

 

 

 

public 

 

 

void MoveMapToLocation(double longitude, double latitude)
{
    Point worldLocation =
new Point(longitude, latitude);

 

 

 

    // Convert Geodetic to Cartesian coordinates
    Point winCenter = this.theMap.MapProjection.ProjectToMap(worldLocation);
    this.theMap.WindowCenter = winCenter;
}

Does anyone have any ideas?

Thanks in advance.

Parents
No Data
Reply
  • 1765
    Verified Answer
    posted

    Hi Michael

     

    I think that you have to convert your Longtitude and Latitude to a rectangle, and after that to set that rectangle as XamMap.WindowRect.

    Please try the following code:

     

            private XamMap map;

            private Rect NextWindowRect { get; set; }

     

     

            public void DisplaySelectedLocation(double Longitude, double Latitude)

                  {

                Rect currentWindowRect = this.map.WindowRect;

                this.NextWindowRect = this.GetRecForLatitudeLongitude(Longitude - 4, Latitude + 4, Longitude + 4, Latitude - 4);

               

                this.map.WindowRect = this.NextWindowRect;

     

                  }

     

     

                  private Rect GetRecForLatitudeLongitude(double topLeftX, double topLeftY, double bottomRightX, double bottomRightY)

                  {

                Point winTopLeft = this.map.MapProjection.ProjectToMap(new Point(topLeftX, topLeftY));

                Point winBottomRight = this.map.MapProjection.ProjectToMap(new Point(bottomRightX, bottomRightY));

     

                         return new Rect(Math.Min(winTopLeft.X, winBottomRight.X),

                                             Math.Min(winTopLeft.Y, winBottomRight.Y),

                                             Math.Abs(winTopLeft.X - winBottomRight.X),

                                             Math.Abs(winTopLeft.Y - winBottomRight.Y));

                  }

     

     

     

    Best regards,

    Anatoli Iliev

Children