Hello everyone. Currently I'm working on project in Xamarin Forms but all I care is that app works on Android devices. User have to be able to mark his position on map (map is loaded as .jpg image) by tapping on it, or eventually by moving point by clicking on buttons. I have no idea how to do that since i did not find any NuGet that allows me to get tap coordinates (Mr. Gestures might do that but as far as I know it is not free). Image does not take whole screen so point (0,0) of image is not same as (0,0) of screen. I would be grateful for any tips or suggestions and I hope for quick response. Thanks in advance.
Forms doesn't support to get the coordinates from any gestures. I think Mr. Gestures could be a good choice.
We need to implement it on the Android part using Custom Renderer if you want to use a free one. Create the custom renderer for your image and get the x/y there:
[assembly: ExportRenderer(typeof(Image), typeof(CustomImageRenderer))] namespace App.Droid { public class CustomImageRenderer : ImageRenderer, IOnTouchListener { public CustomImageRenderer(Context context) : base(context) { } protected override void OnElementChanged(ElementChangedEventArgs<Image> e) { base.OnElementChanged(e); SetOnTouchListener(this); } public bool OnTouch(Android.Views.View v, MotionEvent e) { var x = e.GetX(); var y = e.GetY(); return false; } } }
Answers
Forms doesn't support to get the coordinates from any gestures. I think Mr. Gestures could be a good choice.
We need to implement it on the Android part using Custom Renderer if you want to use a free one. Create the custom renderer for your image and get the x/y there:
Actually, is it possible to do same thing with ScrollView ? Thing is, my image might not fit, and when clicking upper left corner of ImageView i always get (X,Y) = (0,0), even if Image is scrolled. I implemented custom renderer, its same as the renderer You wrote, but it rarely respondes to tapping. I think that its because of nested Image. Is there way to solve it ?
Im stupid. Nesting Image inside of ScrollView is exacly what I need. Thanks for help LandLu.