简体   繁体   English

如何在 Xamarin.Forms.Map 中获取当前位置或移动到当前位置

[英]How to get current location or move to current location in Xamarin.Forms.Map

Since the Map already shows the user location (with IsShowingUser) I just want to zoom to this location.由于地图已经显示了用户位置(使用 IsShowingUser),我只想缩放到这个位置。 Is this easily possible or do I need to get the location on every platform, since I don't find any GeoLocation object.这很容易实现还是我需要在每个平台上获取位置,因为我没有找到任何 GeoLocation 对象。 Only the GeoCoder... Is this not a common usecase to zoom to users position?只有地理编码器......这不是缩放到用户位置的常见用例吗?

You will need to call MoveToRegion method with the position you are interested in.您将需要使用您感兴趣的位置调用MoveToRegion方法。

You can use Geolocator Plugin for Xamarin to get the location in PCL project:您可以使用Geolocator Plugin for Xamarin获取 PCL 项目中的位置:

var locator = CrossGeolocator.Current;
var position = await locator.GetPositionAsync(10000);
map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position. Longitude), 
                                             Distance.FromMiles(1)));

Updated: Xamarin Forms now includes by default Xamarin.Essentials: Geolocation更新:Xamarin Forms 现在默认包含Xamarin.Essentials: Geolocation

Center the map on your location:将地图置于您所在位置的中心:

var position = await locator.GetPositionAsync(5000);
map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(position.Latitude, position.Longitude), Distance.FromMiles(1))

Zoom the map on its current position:在当前位置缩放地图:

var zoomLevel = 9; // between 1 and 18
var latlongdegrees = 360 / (Math.Pow(2, zoomLevel));
map.MoveToRegion(new MapSpan (map.VisibleRegion.Center, latlongdegrees, latlongdegrees));

Ref: https://developer.xamarin.com/guides/xamarin-forms/working-with/maps/参考: https : //developer.xamarin.com/guides/xamarin-forms/working-with/maps/

Is this not a common usecase to zoom to users position?这不是缩放到用户位置的常见用例吗?

Yes, it is.是的,是的。 For iOS just use the MKMapView ShowUserLocation property.对于 iOS,只需使用 MKMapView ShowUserLocation属性。

From Apple documentary:来自苹果纪录片:

Setting this property to YES causes the map view to use the Core Location framework to find the current location and try to display it on the map.将此属性设置为 YES 会导致地图视图使用核心位置框架来查找当前位置并尝试将其显示在地图上。

Source: https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/#//apple_ref/occ/instp/MKMapView/showsUserLocation来源: https : //developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/#//apple_ref/occ/instp/MKMapView/showsUserLocation

So how can you do it in Xamarin?那么如何在 Xamarin 中做到这一点呢? You will need a custom renderer which extends Xamarin Forms MapRenderer .您将需要一个扩展 Xamarin Forms MapRenderer的自定义渲染器。 Get the native map and set the ShowUserLocation property to true.获取本机地图并将 ShowUserLocation 属性设置为 true。

Here is an example:下面是一个例子:

private void MoveToCurrentPosition()
{
  var nativeMap = Control as MKMapView;
  if (nativeMap == null)
    return;

  nativeMap.ShowsUserLocation = true;
}

Also set nativeMap.SetUserTrackingMode(MKUserTrackingMode.Follow, true);同时设置nativeMap.SetUserTrackingMode(MKUserTrackingMode.Follow, true); to automatically follow the user.自动关注用户。

This may not work when the map is initialised, since iOS couldn't get the user location yet.这在地图初始化时可能不起作用,因为 iOS 还无法获取用户位置。 So if you need to display the current user location when the map appears, just use an EventHandler for MKUserLocationEventArgs and use the DidUpdateUserLocation event.因此,如果您需要在地图出现时显示当前用户位置,只需为MKUserLocationEventArgs使用 EventHandler 并使用DidUpdateUserLocation事件。 You can do it like this:你可以这样做:

private EventHandler<MKUserLocationEventArgs> _didUpdateUserLocationEventHandler;

// In OnElementChanged:
_didUpdateUserLocationEventHandler = (_, __) =>
{
    MoveToCurrentPosition();
    nativeMap.DidUpdateUserLocation -= _didUpdateUserLocationEventHandler;
};
nativeMap.DidUpdateUserLocation += _didUpdateUserLocationEventHandler;

Do not forget to deregister this event.不要忘记注销此事件。 Use it just for a usual initialise behaviour.仅将其用于通常的初始化行为。 The user should be free to scroll on the map as he likes.用户应该可以随心所欲地在地图上滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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