如何在 Xamarin.Forms.Map 中获取当前位置或移动到当前位置 [英] How to get current location or move to current location in Xamarin.Forms.Map
问题描述
由于地图已经显示了用户位置(使用 IsShowingUser),我只想缩放到这个位置.这很容易实现还是我需要在每个平台上获取位置,因为我没有找到任何 GeoLocation 对象.只有地理编码器...这不是缩放到用户位置的常见用例吗?
Since the Map already shows the user location (with IsShowingUser) I just want to zoom to this location. Is this easily possible or do I need to get the location on every platform, since I don't find any GeoLocation object. Only the GeoCoder... Is this not a common usecase to zoom to users position?
推荐答案
您将需要使用您感兴趣的位置调用 MoveToRegion
方法.
You will need to call MoveToRegion
method with the position you are interested in.
您可以使用 Xamarin 的地理定位器插件 获取 PCL 项目中的位置:
You can use Geolocator Plugin for Xamarin to get the location in PCL project:
var locator = CrossGeolocator.Current;
var position = await locator.GetPositionAsync(10000);
map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position. Longitude),
Distance.FromMiles(1)));
更新:Xamarin Forms 现在默认包含 Xamarin.Essentials:地理位置
Updated: Xamarin Forms now includes by default Xamarin.Essentials: Geolocation
这篇关于如何在 Xamarin.Forms.Map 中获取当前位置或移动到当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!