Reputation: 9684
I am trying to create a popup dialog which allows users to select coordinates within Australia, however I am having trouble finding specific documentation for the WPF control, even though it is quite similar to the Silverlight control.
Basically what I want to do is center the map on Australia and then zoom to a level of 3.8 and after doing so prevent the user from scrolling the map outside of a range of Australian coordinates, zooming out further then 3.8 or re-centering the map on another location outside of the australian range.
Here is my code so far:
<Window x:Class="GetP51.Views.Dialogs.SelectCoordinatesDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
Title="GetP51 - Select Coordinates" MinHeight="525" Height="525" MaxHeight="525" MinWidth="500" Width="500" MaxWidth="500" Icon="../../GetP51.ico" WindowStartupLocation="CenterOwner" WindowStyle="ToolWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox x:Name="AddressSearch" Grid.Row="0"></TextBox>
<m:Map x:Name="AustralianMap" Grid.Row="1" CredentialsProvider="key" Mode="Aerial"></m:Map>
</Grid>
</Window>
And the code behind:
public partial class SelectCoordinatesDialog : Window
{
public SelectCoordinatesDialog()
{
InitializeComponent();
AustralianMap.Center = new Location(-25.274398, 133.775136);
AustralianMap.ZoomLevel = 3.8;
}
}
Can someone please tell me how to accomplish what I am looking to do?
Thanks, Alex.
Upvotes: 0
Views: 599
Reputation: 1425
You can disable the map's corresponding events by handling them yourself or you can try using the following code to restrict map movement to a given range of values.
The following code will constrain the map to the provided coordinates and also restrict it to the provided zoom levels. You can just switch back and forth between using Aerial
and CustomMode
if you need to.
Note: This is Silverlight code and I have never messed with the map controls in WPF, so I am not positive if this will work.
public class CustomMode : MercatorMode {
protected static Range<double> validLatitudeRange = new Range<double>(-45.58328975600631, -8.320212289522944);
protected static Range<double> validLongitudeRange = new Range<double>(110.7421875, 156.533203125);
protected override Range<double> GetZoomRange(Location center) {
return new Range<double>(3, 3.8);
}
public override bool ConstrainView(Location center, ref double zoomLevel, ref double heading, ref double pitch) {
bool isChanged = base.ConstrainView(center, ref zoomLevel, ref heading, ref pitch);
double newLatitude = center.Latitude;
double newLongitude = center.Longitude;
if(center.Longitude > validLongitudeRange.To) {
newLongitude = validLongitudeRange.To;
} else if(center.Longitude < validLongitudeRange.From) {
newLongitude = validLongitudeRange.From;
}
if(center.Latitude > validLatitudeRange.To) {
newLatitude = validLatitudeRange.To;
} else if(center.Latitude < validLatitudeRange.From) {
newLatitude = validLatitudeRange.From;
}
if(newLatitude != center.Latitude || newLongitude != center.Longitude) {
center.Latitude = newLatitude;
center.Longitude = newLongitude;
isChanged = true;
}
Range<double> range = GetZoomRange(center);
if(zoomLevel > range.To) {
zoomLevel = range.To;
isChanged = true;
} else if(zoomLevel < range.From) {
zoomLevel = range.From;
isChanged = true;
}
return isChanged;
}
}
Upvotes: 1
Reputation: 128062
You could simply disable the map control and overlay it with a transparent control (e.g. a Canvas) that gets mouse input (e.g. MouseLeftButtonUp):
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox x:Name="AddressSearch" Grid.Row="0" />
<m:Map Name="AustralianMap" Grid.Row="1"
ZoomLevel="3.8" Center="-25.274398,133.775136"
IsEnabled="False" />
<Canvas Grid.Row="1" Background="Transparent"
MouseLeftButtonUp="Canvas_MouseLeftButtonUp" />
</Grid>
In the input event handler you could get a Location like this:
private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Location loc = AustralianMap.ViewportPointToLocation(e.GetPosition(AustralianMap));
System.Diagnostics.Trace.TraceInformation("Hit location {0}", loc);
}
And here is the Bing Maps WPF Control API Reference.
Upvotes: 1