简体   繁体   中英

How can you translate a map latitude and longitude to a pixel coordinate using Xamarin.Forms.GoogleMaps?

I have to translate a latitude and longitude shown on the map to a screen coordinate.

I've searched around the web for solutions and it seems like there used to be something called Projection which had a solution for this but it's no longer accessible through Xamarin.Forms.GoogleMaps.

It would be optimal if there was some sort of method to translate coordinates between lat and long to screen- x and y.

I just did a test in the mapRender of Xamarin.Forms.Maps in Android.project and found you can use Projection in the renderer.

protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {

                var projection = NativeMap.Projection;

                projection.ToScreenLocation();
                projection.FromScreenLocation();
            }

            if (e.NewElement != null)
            {
                var formsMap = (CustomMap)e.NewElement;
                customPins = formsMap.CustomPins;
                Control.GetMapAsync(this);
            }
        }

Then you can call projection.ToScreenLocation(); and projection.FromScreenLocation(); to translate the points. I think it's almost same if you use a Xamarin.Forms.GoogleMaps .

Refer: Projection and custom-renderer/map

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM