How to block bing map pan to coordinate bound? - c#

I'm using bing maps components in C# for WP7 with my custom tiles.
I override Microsoft.Phone.Controls.Maps.TileSource, all ok, I'm able to use my tiles.
But, because don't have entire world, I want to limit pan only in one city bound?
It's correct to use MapDragEventArgs to check and block pan??
private void map_MapPan(object sender, MapDragEventArgs e)
{
Map m = (Map)sender;
// something...?
}
...or I need to override something else? In that case, what do I need to override?
thanks.

I have not worked with Bing Maps on Windows phone 7, so going solely by the documentation, I would do the following:
Always initialize the map such that its view bounding rectangle is within your defined tile area.
Bind to ViewChangeStart event. (or ViewChangeOnFrame, you will have to experiment and figure out which to use)
Inside handler for ViewChangeStart, check if TargetBoundingRectangle properly falls outside of your tile coverage area.
If TargetBoundingRectangle falls outside of your coverage area, figure out a way to cancel the view change or manually set the bounding rectangle back the previous valid state.
Assuming everything works like I expect it to, this should limit the users map navigation to your limited areas and avoid the behavior of the view "Snapping" back to some other place after the user has navigated.

What you can easily do and would be the best approach is to use the ViewChangeEnd event and set back the map to your coverage area.
See the MSDN reference to do this:
http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.maps.core.mapcore.viewchangeend(v=vs.92).aspx

Related

Tangram puzzle application

I am trying to create a WPF application using C# to run on Pixelsense that is basic version of the tangram puzzle. I am able to draw my 7 shapes and translate and rotate them all around the screen.
Could anyone give me advise regarding how I should go about saving the pattern (with shapes in specific positions and orientations) so that when a user creates the pattern next time, the application can match it to the saved one and tell the user if it's correct.
It's a pattern matching and recognition problem that I am trying to solve.
I have been stuck on this for a while now :(
Define the solution as a collection of objects with shapeType, position, and orientation properties. Have the solution include one shape at position 0, 0 and an orientation of 0. Now loop over all the shapes the user has actually placed to find the ones with a shapeType that matches the shape your solution has at 0,0,0. Calculate the position and orientation of every other shape relative to where the user put this one. Compare those values to the rest of your solution. You'll need to experiment with how much tolerance to allow because this stuff is not precise - to make the game fun, err on the side of having high tolerances. If needed, you can follow this up with some performance optimizations to only re-evaluate pieces that moved.
Hopefully you are using physical shape prices with tags on them instead of this purely a virtual game. I always wanted to build this when I was on the Surface team but it never happened. One challenge you will run into is defining how the tag's position/orientation relates to the actual shape. If you'll be putting tag stickers on multiple tangram sets, you almost certainly won't get the on precisely the same each time so you may need to add a "calibration" mode to your app (have the user place each piece in a specific spot and then push a button so you can record where the tag is relative to those spots). The TagVisualizer WPF control should help a lot for building your UI - definitely look into using it (this scenario was top of mind when we designed that API). The default behavior of that control (if you tell it the ID of a tag to look for but not how to visualize it) is a "crosshair" that can help you find tune your offset values.
Good luck! If you wouldn't mind recording a YouTube video when you are done and posting a comment here linking to it, I'd really appreciate that
You can use ObservableCollection or List of a custom class. That class can consist of various values such as position, orientation etc as properties.
When a new pattern is drawn or when the pattern change its position you can update that particular object stored in the collection. As you have all the details of the pattern(positions and orientation) you can iterate the for loop and check the position of the new pattern when added.

AutoCAD: Access group pivot in C#

I am writing a plugin for AutoCAD in C#.
I would like to change the position of the pivot point of a group.
It is possible to do that by mouse clicking but after deselecting and reselecting the group, it automatically moves back to it's default position (which looks like it is the center point of the bounding box).
If I use something like...
private void SetBasePoint(Group group)
{
group.AnyAvailableMethod();
//or...
group.AnyAvailableAttribute;
}
...none of the offered attributes or methods give me any solution or hint how to get there.
Does anyone have a clue how to access the pivot?
-Mike
So after working through several APIs and websites I contacted Kean Walmsley from Autodesk Developer Network and even he told me there was no direct way to get access to the pivot of a group.
So now I'm saving a user defined basepoint inside AutoCAD and whenever I need a function with a predefined pivot position I simply call my own loadBasepoint() -method which fits perfectly for my specific case.
Just in case someone runs into the same issue I thought I'd mention it :)

Windows Phone 8 - Map setView

I tried to display all my GeoCoordinate-Points on my map after loading. I found out, that i can use the setView method of the Map. I used it like this:
delMap.SetView(LocationRectangle.CreateBoundingRectangle(viewLocations));
viewLocations is a GeoCoordinate[] with 6 points. But for some reasons this is not working. The map still just centers at the point which was drawn last.
I call this in the onNavigatedTo method of the page, after drawing all the points of the locations on the map.
Anyone an idea why this is not working? Or is there a better possibility to calculate the zoom factor i need to see all the points in one view after loading.
The problem was, that I called it in the onNavigatedTo-Method. I don`t really know why this is a problem, but however, after i put it in the loaded method, it worked fine.

How do I programmatically position a canvas in Silverlight?

I'm using Silverlight 3/C#, and I'm trying to create some XAML objects programatically in response to a user clicking on a button.
However, I can't seem to position a Canvas object that I have created - when I call SetValue() on the new Canvas with the Canvas.LeftProperty value, the browser window clears to an empty screen.
I have a simple function that exhibits the problem (I started out with something more complex):
private Canvas MakeNewCanvas()
{
Canvas newCanvas = new Canvas();
newCanvas.Width = newCanvas.Height = 50;
newCanvas.SetValue(Canvas.LeftProperty, 10);
return newCanvas;
}
and a simple button click handler that calls this:
private void MyButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
myPage.Children.Add(MakeNewCanvas());
}
NB: myPage is a Canvas object defined in my app's MainPage XAML file.
I've traced this through in the VS debugger with the SL app executing in Firefox, and whenever it executes the SetValue() line, the browser goes white and starts trying to download data (according to the status bar). I tried calling SetValue() after I've put the Canvas in the XAML tree with something like:
myPage.Children.Add(newCanvas);
newCanvas.SetValue(Canvas.LeftProperty, 10);
but it makes no difference - as soon as the SetValue() call is hit, the browser goes white.
The Canvas class seems to have no direct method of setting Left/Top on itself, apart from the SetValue() function with dependency property enum values. There's also Canvas.SetLeft() and Canvas.SetTop() but I guess they're just shims onto SetValue(). Using them didn't help anyway.
If I don't call SetValue(), then my canvas appears (and child objects I've added to it) in the SL app as you might expect, in the very top left.
If I create and position a Canvas object in Expression Blend, then the XAML generated includes Canvas.Left and Canvas.Top property values on the Canvas itself, as I would expect, so it seems to me that what I'm trying in C# should work.
But I don't seem to be able to set the left/top values myself from C# without the SL in the browser going all weird.
Any ideas?
Edit: my approach is correct, but canvas coords need to be floating point, not integer - see accepted answer for details.
Trying your code, but with the debugger catching exceptions, I get:
DependencyProperty of type System.Double cannot be set on an object of type System.Int32.
which is a really stupid gotcha - SetValue only takes Object, so you're prone to a problem like this.
Try either:
newCanvas.SetValue(Canvas.LeftProperty, 10.0);
or
Canvas.SetLeft(newCanvas, 10);
and it will probably work.
The behaviour of attached properties can be a little confusing at first.
The properties Canvas.LeftProperty and Canvas.TopProperty are applied to child objects of a canvas. They are therefore only meaningful when the child object is placed on a Canvas. Its important to understand the in WPF/SL objects do not position themselves, its up to the containing panel to decide where to put them.
I suspect the myPage is not of the Canvas type, its probably a Grid, hence it would have no idea what to do with such properties even if it bothered to look for them (which it doesn't).
In order for you to specifically position you new Canvas you need to be adding it to a Canvas.

Projector Control/Display C#

I've had a bit of a search around, but couldn't find anything similar to what I was looking for. I'm interested in knowing how to display differing contents on secondary/tertiary monitors or projectors using C#. Basically, what I'm wanting to achieve is to have some form of presenter view (a lá Powerpoint) on one particular screen (the primary display), and the output on a secondary screen or projector. I've never really attempted to develop something with multiple display outputs, so any guidance will probably have to be at a fairly obvious level.
If someone could point me in the right direction as to how to handle this sort of thing in C#, that would be greatly appreciated!
Just to expand on Keven's answer (I +1'd it), The Screen.AllScreens array gives you have an array of Screen objects. The Screen object has a property called IsPrimary, which you can use to determine which is the primary screen, and which is the secondary (duh) and it also has a property called WorkingArea, which is a rectangle that gives you back all the coordinates of the second screen. The cool thing about this is, that even if let's say the secondary screen is configured to be on the left of the primary, the WorkingArea.X will be a negative number, and you can place forms there or whatever.
You can use the System.Windows.Forms.Screen.AllScreens property to access a list of all the monitors Windows knows about. If you're looking to utilize a display that hasn't been configured by the user, it gets more difficult - you'd probably need to initialize and access the display adapter using DirectX.
One of the main classes you will need to interact with is Screen (this is in the WinForms namespace). In general all the screens are treated as a set of working areas that you can use the screen class to get properties for each one.
You can get all the screens like this...
Screen [] screens = Screen.AllScreens;
Here is a short article about doing multi-monitor programming in C#.
Also remember to set the property 'StartPosition' to 'Manual' on your the window you want to display on the secondary screen otherwise it will always display on the primary screen.

Categories

Resources