Xamarin.Foms GoogleMaps MoveToRegion不起作用 [英] Xamarin.Foms GoogleMaps MoveToRegion doesn't work

查看:41
本文介绍了Xamarin.Foms GoogleMaps MoveToRegion不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在构造函数命令

map.MoveToRegion(defaultPin)

其中地图需要显示一个地图,其默认图钉位于中心.

where map needs to present a map with default pin in center.

这是我的代码隐藏类的一部分:

Here is a part of my codebehind class:

public partial class LocationPage : PostLoginContentPage
{
    public LocationPage ()
    {
        BindingContext = ViewModel = new LocationViewModel(App.ScreenHeight, App.ScreenWidth);
        InitializeComponent ();
        searchFullMapImage.IsVisible = false;
        map.UiSettings.ZoomControlsEnabled = false;
        List<Pin> defaultPins = ViewModel.Pins;
        Pin firstPinPosition = defaultPins?.FirstOrDefault(m => m.Position != null && m.Position.Latitude != 0 && m.Position.Longitude != 0);

        LoadPins(defaultPins);
        map.SelectedPin = firstPinPosition;
        map.MoveToRegion(MapSpan.FromCenterAndRadius(firstPinPosition.Position, Distance.FromMeters(10000)), true);
    }

 //some other methods...
}

此外,我如何绑定onClick的按钮以向我展示罗马?我正在使用XF.GoogleMaps 2.3.0和Xamarin.Forms 2.5.4.444

Also, How can I bind onClick on a button to show me a Rome for example? I am using XF.GoogleMaps 2.3.0 and Xamarin.Forms 2.5.4.444

推荐答案

好,为了使其正常工作,您需要在移动地图之前确保地图已准备就绪.我建议将您的代码移至viewModel并在那里绑定属性.因此,您可以检查mapReady(很抱歉,如果属性名称不同,但类似的名称)

Ok, for it to properly work you need to assure the map is ready before moving the map. I'd suggest moving your code to a viewModel and binding the properties there. So you could check for mapReady (sorry if the property name is different but its something like it)

然后确保地图准备就绪并且可以移动.您应该确保它在UI线程上执行,否则它不会移动.

Then after you make sure the map is ready and can move. You should make sure it is executed on the UI thread otherwise it wont move.

为了确保您可以使用它,

To assure it will you can use,

Device.BeginInvokeOnMainThread (() => {
          map.MoveToRegion(MapSpan.FromCenterAndRadius(firstPinPosition.Position,
Distance.FromMeters(10000)), true);
});

这篇关于Xamarin.Foms GoogleMaps MoveToRegion不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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