Get screen coordinate on user click

Akshay Dixit 21 Reputation points
2020-12-08T04:20:01.66+00:00

Hello @Everyone, This is one of the screens on which I have to stick one image on another. Like, in the picture above There is an image of the building topside/ schematic of the Building. On that image where the user clicks those pin(circle image) type images popup. So what I need is the screen coordinates of the user who clicks on that photo. If I get the Screen coordinate then on specific coordinate I will popup those Pin Images. That is my Question. I am waiting for your working responses guys45997-screenshot-7.png

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-12-08T07:25:32.093+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Do you want to achieve the result like following GIF?

    45990-navi.gif

    If so, you can install TouchTracking.Forms nuget packages. add Effect for the SKCanvasView

       <AbsoluteLayout x:Name="myabs">  
               
                   <views:SKCanvasView  
                       AbsoluteLayout.LayoutBounds="0,0,1,1"  
                        AbsoluteLayout.LayoutFlags="SizeProportional"  
                         
                       x:Name="canvasView" >  
                       <views:SKCanvasView.Effects>  
                           <tt:TouchEffect Capture="True"  TouchAction="TouchEffect_TouchAction" />  
                       </views:SKCanvasView.Effects>  
                   </views:SKCanvasView>  
              
           </AbsoluteLayout>  
         
       Here is layout backgroud code  
         
         private void TouchEffect_TouchAction(object sender, TouchTracking.TouchActionEventArgs args)  
               {  
                    
                   var x = args.Location.X;  
                   var y = args.Location.Y;  
                   var v = new View1();  
                   AbsoluteLayout.SetLayoutBounds(v, new Rectangle(x, y, 0.25, 0.25));  
                   AbsoluteLayout.SetLayoutFlags(v, AbsoluteLayoutFlags.SizeProportional);  
                   
                   myabs.Children.Add(v);  
               }  
    

    when we click the screen, we get the X/Y point, then Add our custom control.

    I make an custom control.

       <ContentView xmlns="http://xamarin.com/schemas/2014/forms"   
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                    x:Class="SkiaSharpDemo.View1">  
         <ContentView.Content>  
             <StackLayout>  
                   <ImageButton WidthRequest="50" HeightRequest="50" BackgroundColor="Transparent" Source="hamburger.png" Clicked="ImageButton_Clicked" ></ImageButton>  
                   <Frame IsVisible="false" x:Name="myframe"  WidthRequest="100" HeightRequest="70">  
                       <StackLayout>  
                           <Label FontSize="Small" Text="Edit"/>  
                           <Label FontSize="Small" Text="Duplicate"/>  
                           <Label FontSize="Small" Text="Remove"/>  
                       </StackLayout>  
                   </Frame>  
             </StackLayout>  
         </ContentView.Content>  
       </ContentView>  
         
       //background code  
        public partial class View1 : ContentView  
           {  
               public View1()  
               {  
                   InitializeComponent();  
               }  
         
               private void ImageButton_Clicked(object sender, EventArgs e)  
               {  
                   myframe.IsVisible = !myframe.IsVisible;  
               }  
           }  
    

    I do not judge the border of the views:SKCanvasView, you can judge it by yourself.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Vaibhav Patil 11 Reputation points
    2020-12-08T06:51:42.43+00:00

    Will this work ?

     public  bool OnTouch(View v,Android.Views.MotionEvent e)
      {
                float x = e.GetX();
                float y = e.GetY();
                return false;
      }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.