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.
I was able to get the symbol to work using your process outlined above.
I can move ahead now that I know how even if I don't know why.
Thanks.
This worked, but only after I made changes to my WorldRect as well. I had to set my WorldRec to use:
theMap.WorldRect = GetRecForLatitudeLongitude(longitude - 16, latitude + 16, longitude + 16, latitude - 16);
This centered it on the US and set the minimum zoom level (which is what I want), but I don't understand why. I had some code that set the WorldRect earlier on in the code chain, but I got an error about it already being a child element.
If I don't set the WorldRect, my map won't zoom, pan, or do anything else. If I set it to top left: -180, 90 and bottom right: 180, -90, then it centers to 0,0 instead of the coordinates I set using your code.
Also, not sure if this is related, but I can't set symbols using the ProjectToMap(new Point(longitude,latitude)).
I guess they're related in that I'll have to work some magic with the lat/long to get the aymbol to set correctly.
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