WP7 dragging a pushpin on a map - silverlight-4.0

Does anyone have any insight into how to implement a draggable pushpin on a map on a WP7 client running Mango? I have a pushpin bound to a geo-location on a map and I want the user to be able to drag it on a map and record its new location. I've seen some resources, but they're for non-WP7 Bing Maps control. Any help would be appreciated.
Thanks!

e.GetPosition(element) gives the position relative to the element passed on the parameter. Also, the point to convert using ViewportPointToLocation has to be relative to the position of the map. So, you have to do the following:
Pushpin myPin = sender as Pushpin;
Point p = e.GetPosition(**Map**);
g = Map.ViewportPointToLocation(p);

I know this is a late response but I was working on a similar project and using a similar approach, so I thought I'd share. The approach that I used was:
Create two global double variables to hold X,Y coord's:
double mapLocX;
double mapLocY;
Set these global doubles to the location of the point of your pushpin in your DragStarted event:
Point point = myMap.LocationToViewportPoint(myPin.Location);
mapLocX = point.X;
mapLocY = point.Y;
In your dragDelta event, change these variables as your would your pushpin:
mapLocX += e.HorizontalChange;
mapLocY += e.VerticalChange;
Now on DragCompleted create a new point that takes in our rendered global variables, and map them to a geocoordinate, and here's the kicker; Remove our old pin from the ObservableCollection (Mine is Locations) and add in a new pushpin at our new coordinate:
Point point = new Point(mapLocX, mapLocY);
GeoCoordinate geoCoord = new GeoCoordinate();
geoCoord = myMap.ViewportPointToLocation(point);
Locations.Remove(myPin.Location);
Locations.Add(geoCoord);
Hope this helps

If anyone is curious, this is the solution I came up with:
XAML:
<map:Map x:Name="Map" Height="400" HorizontalAlignment="Left" Margin="6,10,0,0" VerticalAlignment="Top" Width="444" ZoomLevel="19" >
<map:Map.Mode><map:AerialMode /></map:Map.Mode>
<map:Pushpin x:Name="Pin" Background="Green" IsHitTestVisible="True" IsEnabled="True">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener DragDelta="Pushpin_OnDragDelta" DragStarted="Pushpin_OnDragStarted" DragCompleted="Pushpin_OnDragCompleted">
</toolkit:GestureListener>
</toolkit:GestureService.GestureListener>
<map:Pushpin.RenderTransform>
<TranslateTransform></TranslateTransform>
</map:Pushpin.RenderTransform>
</map:Pushpin>
</map:Map>
.cs:
private void Pushpin_OnDragDelta(object sender, DragDeltaGestureEventArgs e)
{
Pushpin myPin = sender as Pushpin;
TranslateTransform transform = myPin.RenderTransform as TranslateTransform;
transform.X += e.HorizontalChange;
transform.Y += e.VerticalChange;
}
private void Pushpin_OnDragStarted(object sender, DragStartedGestureEventArgs e)
{
Map.IsEnabled = false; // prevents the map from dragging w/ pushpin
}
private void Pushpin_OnDragCompleted(object sender, DragCompletedGestureEventArgs e)
{
Map.IsEnabled = true;
}
Does anyone have any ideas on how to extract the geocoordinates of the pushpin's new location?? I tried the code below in the event handler and it doesn't give correct coordinates:
Pushpin myPin = sender as Pushpin;
Point p = e.GetPosition(myPin);
g = Map.ViewportPointToLocation(p);
myPin.location gives the old coordinates

You want to have the origin of your UI element not your finger right? Try this:
Point pM = e.GetPosition(**Map**);
Point pE = e.GetPosition(**UIElement**);
Point origin = new Point(pM.X - pE.X, pM.Y - pE.Y);
GeoCoordinate g = Map.ViewportPointToLocation(origin);

Related

UWP Reorder Gridviewitems on Xbox

I am making a UWP app which is supposed to be on xbox for now and maybe in future ill release it on pc and other platforms. I know that on PC and for mobile we can enable this feature with following 2 properties on the GridView or ListView.
CanReorderItems=True
CanDrop=True
But according to Microsoft Docs, drag and drop feature is not available or supported on xbox.
So what are any other options to achieve this reorder feature on xbox GridView?
UPDATE 1
So here is my backend code for the gridview. selection mode is single but I am not using selectionchanged event because that just creates lot of confusion and for now just assume that we always need to swap the items I will set the boolean later once the swapping in working perfectly.
private void SamplePickerGridView_ChoosingItemContainer(Windows.UI.Xaml.Controls.ListViewBase sender, ChoosingItemContainerEventArgs args)
{
if (args.ItemContainer != null)
{
return;
}
GridViewItem container = (GridViewItem)args.ItemContainer ?? new GridViewItem();
//should be xbox actually after pc testing
if (DeviceTypeHelper.GetDeviceFormFactorType() == DeviceFormFactorType.Desktop)
{
container.GotFocus += Container_GotFocus;
container.LostFocus += Container_LostFocus;
//container.KeyDown += Container_KeyDown;
}
args.ItemContainer = container;
}
private TVShow GotItem, LostItem;
private void Container_LostFocus(object sender, RoutedEventArgs e)
{
LostItem = OnNowAllGridView.ItemFromContainer(e.OriginalSource as GridViewItem) as TVShow;
GotItem = null;
}
private void Container_GotFocus(object sender, RoutedEventArgs e)
{
GotItem = OnNowAllGridView.ItemFromContainer(e.OriginalSource as GridViewItem) as TVShow;
if (GotItem != null && LostItem != null)
{
var focusedItem = GotItem;
var lostitem = LostItem;
var index1 = ViewModel.Source.IndexOf(focusedItem);
var index2 = ViewModel.Source.IndexOf(lostitem);
ViewModel.Source.Move(index1, index2);
}
LostItem = null;
}
u can try the code with adaptivegridview or just normal gridview of uwp if it works with that it should work with adaptivegridview as well.
Current Bheaviour items are swaped but the focus remains at same index.
Expected the focus should also move along with the item.
Your finding is true, drag and drop is not supported on Xbox out of the box (although when mouse support comes to Xbox in the future, I guess it will work).
So if you need this functionality, you will have to implement it manually from the start. One option would be to add a button, that will display on Xbox only and will read like Reorder Grid.
When this "reorder" mode were enabled, you have several solutions available.
The easiest solution for you would be to set the SelectionMode to Single and when a item is selected, you would bring it to fromt of the underlying collection.
collection.Remove( selectedItem );
collection.Insert( 0, selectedItem );
This bring to front solution was implemented on the Xbox One dashboard for reordering tiles.
Second option would be to set the SelectionMode to Multiple, where user would first select one item and then a second one. After that you could move the first selected item before the second selected:
collection.Remove( firstSelectedItem );
var targetIndex = collection.IndexOf( secondSelectedItem );
collection.Insert( targetIndex, firstSelectedItem );
The last solution is the most complex. With SelectionMode = Single you would select a single item and then observe the direction in which the user focus moves and move the tile "in real time". This is the most user friendly, but hardest to implement reliably.
Just as an outline of the third solution - you could capture the GotFocus event if you implement a custom template of the GridView:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal"
GotFocus="GridViewItem_GotFocus"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
Now within this GotFocus handler you could retrieve the item that has currently focus from the EventArgs.OriginalSource. This way you could know which item got the focus and you could swap it with the item the user selected.
Update - hacky solution
I have come up with a hacky approach that solves the GotFocus/LostFocus mess.
The problem with GotFocus is that when we move the item in collection, the focus gets confused. But what if we didn't physically move the items at all?
Suppose your item type is TVShow. Let's create a wrapper around this type:
public class TVShowContainer : INotifyPropertyChanged
{
private TVShow _tvShow;
public TVShow TvShow
{
get => _tvShow;
set
{
_tvShow = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Now change the collection item type to this new "wrapper" type. Of course, you also have to update your GridView DataTemplate to have the right references. Instead of "{Binding Property}" you will now need to use "{Binding TvShow.Property}", or you can set the DataContext="{Binding TvShow}" attribute to the root element inside the DataTemplate.
But you may now see where I am going with this. Currently you are using Move method to move the items in the collection. Let's replace this with a swap:
var item1 = focusedItem.TvShow;
focusedItem.TvShow = LostItem.TvShow;
LostItem.TvShow = item1;
This is a big difference, because we no longer change the collection itself, but just move the references to items that are wrapped in a "static" container. And thanks to bindings the items will properly display where they should.
This is still a hacky solution, because it requires you to wrap your items just for the sake of the reordering, but it at least works. I am however still interested in finding a better way to do this.

UWP/c++-cx - Moving a button on the vertical axis by dragging it

I have a Grid which is as high as the application and has the width of 50. I have got a button in it on the left top with the width of 50 also. I want to move this button along the vertical left axis by dragging it with the mouse. But it should be stil able to be clicked normally. I tried to do this with the drag-and-drop sample by microsoft but the procedure I want to implement is not quite drag-and-drop. How can I implement this by using XAML and c++-cx as code behind in an universal windows app ?
My XAML-Code for the Grid/Button:
<Grid x:Name="Grid1" Width="50" >
<Button x:Name="btnMove" Content="Move me!" Background="PaleGreen" Click="btnMove_Click" VerticalAlignment="Top" HorizontalAlignment="Left" Width="50" Height="50"></Button>
</Grid>
For your requirement, you could move the button on the vertical axis by using ManipulationDelta class. And you could achieve it with the following code.
For more please refer to Handle pointer input. Here is official code sample.
MainPage::MainPage()
{
InitializeComponent();
InitManipulationTransforms();
btnMove->ManipulationDelta += ref new ManipulationDeltaEventHandler(this, &MainPage::btnMove_ManipulationDelta);
btnMove->ManipulationMode = ManipulationModes::TranslateX;
}
void App14::MainPage::InitManipulationTransforms()
{
transforms = ref new TransformGroup();
previousTransform = ref new MatrixTransform();
previousTransform->Matrix = Matrix::Identity;
deltaTransform = ref new CompositeTransform();
transforms->Children->Append(previousTransform);
transforms->Children->Append(deltaTransform);
// Set the render transform on the rect
btnMove->RenderTransform = transforms;
}
void App14::MainPage::btnMove_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
}
void MainPage::btnMove_ManipulationDelta(Platform::Object^ sender, ManipulationDeltaRoutedEventArgs^ e)
{
previousTransform->Matrix = transforms->Value;
// Get center point for rotation
Point center = previousTransform->TransformPoint(Point(e->Position.X, e->Position.Y));
deltaTransform->CenterX = center.X;
deltaTransform->CenterY = center.Y;
// Look at the Delta property of the ManipulationDeltaRoutedEventArgs to retrieve
// the rotation, scale, X, and Y changes
deltaTransform->Rotation = e->Delta.Rotation;
deltaTransform->TranslateX = e->Delta.Translation.X;
deltaTransform->TranslateY = e->Delta.Translation.Y;
}
You could change the scrolling direction of the button by modifying the ManipulationMode of button.
btnMove->ManipulationMode = ManipulationModes::TranslateY;

How do I create a tap moveable control in WinRT?

I wrote a UserControl in WinRT and I want to make it moveable with a finger.
When I move it using a pen or mouse it is still moving but not when i use a finger.
The PointerMoved is not triggert when I use a finger.
Here is the simple xaml:
<UserControl>
<Rectangle PointerPressed="PointerPressed" PointerMoved="PointerMoved"/>
</UserControl>
and here is the code:
private Point position;
void PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Rectangle r = sender as Rectangle;
var pointerPoint = e.GetCurrentPoint(r);
position = pointerPoint.Position;
}
void PointerMoved(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
{
Rectangle r = sender as Rectangle;
var delta = e.GetCurrentPoint(r).Position;
r.Margin = new Thickness(r.Margin.Left + delta.X - position.X, r.Margin.Top + delta.Y - position.Y, 0, 0);
}
What do I miss here?
Edit:
I am working with Windows 8.1 and VisualStudio 2013.
Maybe it's a new feature^^
It's simpler than you think!
<Rectangle Width="100" Height="100" Fill="White"
ManipulationMode="TranslateX,TranslateY"
ManipulationDelta="Rectangle_ManipulationDelta_1" />
private void Rectangle_ManipulationDelta_1(object sender, ManipulationDeltaRoutedEventArgs e)
{
var _Rectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
var _Transform = (_Rectangle.RenderTransform as CompositeTransform)
?? (_Rectangle.RenderTransform = new CompositeTransform()) as CompositeTransform;
_Transform.TranslateX += e.Delta.Translation.X;
_Transform.TranslateY += e.Delta.Translation.Y;
}
Best of luck!
First, don't sure you can move by pen or mouse because in PointerMoved event you should check the e.Pointer.IsInContact boolean value to ensure you are selected the object when moving. It make your moving action looks better.
Second, Sorry that I don't know why in your machine the PointerMoved is not triggered when uses finger. Anyway it will better if you set the name of your handler function not same as event name.
If you can share more information, we can discuss.

Context menu in windows 8 using Popup

Hi I am trying to create a context menu in windows 8 using Popup. On Right click of a button I am calling the following function
private async void UIElement_OnRightTapped(object sender, RightTappedRoutedEventArgs e)
{
PopupMenu popUpMenu = new PopupMenu();
popUpMenu.Commands.Add(new UICommand("File"));
Rect rect = GetRect(sender);
var result= await popUpMenu.ShowForSelectionAsync(rect, Placement.Right);
}
While defination for GetRect method is as follows:-
private Rect GetRect(object sender)
{
FrameworkElement element = sender as FrameworkElement;
GeneralTransform elementTransform = element.TransformToVisual(null);
Point point = elementTransform.TransformPoint(new Point());
Size size = new Size(element.ActualWidth, element.ActualHeight);
Rect rect = new Rect(point, size);
return rect;
}
Though the GetRect is returning correct value, but still i am getting result as null.
Please help
I can't figure out why you have this problems. On the first view, everything looks fine.
Maybe Tim Heuer Callisto package contains an easier approach for exactly what you want. See this example.
It's because you're not clicking on the File command, but instead clicking elsewhere on the page.

Is there a way to make binding happen in a Silverlight control that is created in code?

I'm wanting to print a UserControl that binds to a view model. I create the control in code because if I don't it prints off the edge of the page. Apparently, there is no way to control the size or scale of a printed UIElement without screwing up the element that is on the Silverlight page. So I create the 'UserControl' with the following code in the PrintPage event of a PrintDocument:
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
PurchaseReceipt purchaseReceipt = new PurchaseReceipt();
purchaseReceipt.DataContext = _receiptData;
purchaseReceipt.Visibility = System.Windows.Visibility.Visible;
purchaseReceipt.Margin = new Thickness(25.0);
purchaseReceipt.Width = e.PrintableArea.Width - 50.0;
e.PageVisual = purchaseReceipt;
}
The problem is that binding doesn't work, or doesn't work in time. Is there a way to force an element to bind?