如何居中/设置地图缩放以覆盖Xamarin.Forms上可见的所有标记? [英] How to Center/Set Zoom of Map to cover all markers visible on Xamarin.Forms?

查看:110
本文介绍了如何居中/设置地图缩放以覆盖Xamarin.Forms上可见的所有标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的地图设置了多个标记



我可以静态设置缩放级别和中心

但我想要的是居中到所有标记和缩放级别来填充所有可见的标记



我可以使用接收MapSpan的Gmaps.MoveToRegion方法。事情是,我不知道如何计算MapSpan需要显示所有标记。



我使用Xamarin.Forms.Maps和Xam.Plugin。 MapExtend.Abstractions



预先感谢!

解决方案

与下面的C#代码。

  locationManager =(LocationManager)GetSystemService(Context.LocationService); 

Criteria criteria = new Criteria();

criteria.Accuracy = Accuracy.Fine;
criteria.PowerRequirement = Power.Low;

provider = locationManager.GetBestProvider(criteria,true);

location = locationManager.GetLastKnownLocation(provider);

marker1lat = location.Latitude;
marker1lng = location.Longitude;

marker2lat = location.Latitude;
marker2lng = location.Longitude;

LatLng marker1LatLng = new LatLng(marker1lat,marker1lng);
Latlng marker2LatLng = new LatLng(marker2lat,marker2lng);

LatLngBounds.Builder b = new LatLngBounds.Builder()
.Include(marker1LatLng)
.Include(marker2LatLng);

mMap.MoveCamera(CameraUpdateFactory.NewLatLngBounds(b.Build(),120));


I am setting multiple markers to My Map

I can set statically the zoom levels and the center

but what i want is To Center to all markers and zoom level to fill all visible markers

I can use the Gmaps.MoveToRegion method which receives a MapSpan. The thing is, I don't know how to calculate the MapSpan needed to show all the markers.

I'm using Xamarin.Forms.Maps and Xam.Plugin.MapExtend.Abstractions

Thanks in advance!

解决方案

I had success with the below c# code. Just record the position of each marker as a LatLng object.

locationManager = (LocationManager)GetSystemService(Context.LocationService);

Criteria criteria = new Criteria();

criteria.Accuracy = Accuracy.Fine;
criteria.PowerRequirement = Power.Low;

provider = locationManager.GetBestProvider(criteria, true);

location = locationManager.GetLastKnownLocation(provider);

marker1lat = location.Latitude;
marker1lng = location.Longitude;

marker2lat = location.Latitude;
marker2lng = location.Longitude;

LatLng marker1LatLng = new LatLng(marker1lat, marker1lng);
Latlng marker2LatLng = new LatLng(marker2lat, marker2lng);

LatLngBounds.Builder b = new LatLngBounds.Builder()
                    .Include(marker1LatLng)
                    .Include(marker2LatLng);

mMap.MoveCamera(CameraUpdateFactory.NewLatLngBounds(b.Build(), 120));

这篇关于如何居中/设置地图缩放以覆盖Xamarin.Forms上可见的所有标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆