0

I am building a simple app where user can draw freehand on Google map and select markers within it. I am using Xamarin Forms to build this application.

I have tried few things to draw freehand on Google Map, but on mobile screen inside webview it does not work.

So, I used a Xamarin plugin to generate Google map in screen, but this plugin https://github.com/amay077/Xamarin.Forms.GoogleMaps

Does not have freehand drawing too but it has a polylines drawing tool with given positions.

My idea was that if I could overlap a layer on screen and use darw freehand on that layer and convert those points of screen into Maps Lat and Lon, then can pass these Lat/Lon to Map plugin to draw polyline right on map for me.

For drawing on map I am using another plugin of Xamarin forms SkiaSharp, and I have made its background transparent, so, it gives a look like we are just making drawing on directly Map.

So, till here all is perfect, also I have got Lat/Lon of all sides of Map too.

So, I have

  1. SkiaSharp on screen overlayered on Google map which makes drawing on that layer perfect, and map is fully visible in background to give a look like we are drawing on Map
  2. I have Lat/Lon of all four sides of Map (Top, Left, Right, Bottom)
  3. I have points when drawing on SkiaSharp screen

the problem is that I do not know how to convert

The solution I have found here all are related to either only android or web based google maps api,

I need some idea how can I convert my screen points into Google Lat/Lon

I have few ideas but not sure how they will work

  1. Maybe we can use xamarin.forms.maps, and build some middleware to communicate between screen points and xamarin.forms.maps to get coordinates, this might be helpful in case of iOS as well, so, we will get coordinates using default Maps, and pass those coordinates to Google maps plugin to create polylines on map

  2. Maybe a server based API to which we pass screen size, points, maps 4 sides coordinates and that API can return us Lat/Lon against them?

  3. Or some formula inside app to convert X,Y points to Coordinates?

Andreas
  • 2,455
  • 10
  • 21
  • 24
Creative
  • 83
  • 8

1 Answers1

2

Google Maps has a Projection class that has methods can be used to convert screen coordinates to Lat/Long (and back if needed):

yourGoogleMapInstance.Projection.FromScreenLocation(APointObject) 

iOS's MapKit has a similar function (C# MKMapView.ConvertPoint = ObjC convertPoint:toCoordinateFromView:):

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • 1
    Brilliant. I'll have to remember that – Patrick Goode Nov 12 '19 at 18:34
  • But I am using a plugin to load google maps, I am not using native maps, I am not sure if this solution is applicable to my situation as well? – Creative Nov 13 '19 at 15:46
  • I am able to draw perfectly on map using a transparent layer, the problem converting those screen points to lat long – Creative Nov 13 '19 at 15:49
  • @Creative What is the problem? – SushiHangover Nov 13 '19 at 15:56
  • I don't know at all how to proceed from here, how do I convert my screen X and Y into coordinates at all? – Creative Nov 14 '19 at 05:48
  • I have created my own projection class as well someone build that in Java and i changed that into C#, but the X,Y values I get from SkiaSharp are too different then what I get from that projection class, from same point of scree. – Creative Nov 14 '19 at 05:53
  • that class is quite similar to this one, https://gist.github.com/nagasudhirpulla/9b5a192ccaca3c5992e5d4af0d1e6dc4 – Creative Nov 14 '19 at 05:55
  • like when I convert back, Lat Lon to points using projection class, it gives something like this 38618.90844, 49253.0681, which are abousily not screen points at all, because screen full size is something like 360x600, so, screen points are within this range – Creative Nov 14 '19 at 06:04
  • I also tried to use this class, https://stackoverflow.com/questions/12741128/how-to-convert-geographical-coordinates-to-pixels/22784177#22784177 and it is giving very different result compare to above like converting same LatLon to pixels it gives 80380, 106378 – Creative Nov 14 '19 at 06:17
  • @Creative, any update? Do you have solution to solve your issue now? – Cherry Bu - MSFT Nov 19 '19 at 09:24
  • No not yet, I really need it let me know if you find some solution? – Creative Nov 19 '19 at 13:43