Xamarin.Forms.Maps 上的居中固定标记

Centered Fixed Marker on Xamarin.Forms.Maps

我正在尝试在 Xamarin.Forms 上集成一张地图,当打开时,地图的中心会放置一个固定标记。固定的,我的意思是当拖动地图时它不会移动并且标记本身也不可拖动。有人可以指导我完成此操作,或者您有任何我可以阅读的参考资料吗?我已经显示了地图。

不要在地图上添加标记,而是在地图上叠加一个视图。

我遇到了同样的问题,在搜索了很长时间之后,我终于结合了一些羊皮纸和逻辑来得出一个最终可行的解决方案。我 post 在 Github 上为居中标记问题提供了完整的解决方案,这里是 link。

https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/486

但出于冗余目的(以防万一您出于某种原因找不到 post),这里是解决方案

<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <StackLayout x:Name="PanelMain"  AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
            <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="Start">
                <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical"  VerticalOptions="Start" Padding="10,0,10,0">
                    <Label x:Name="lblTotal" FontSize="14" TextColor="Black" FontAttributes="Bold" HorizontalTextAlignment="Center" Text="[Address Show Here]" />
                    <Label x:Name="lblCoordinates" VerticalTextAlignment="Center" FontSize="12" HorizontalTextAlignment="Center" Text="[GPS Coordinates]" />

                    <SearchBar x:Name="SearchTextBox" TextColor="Black" HorizontalOptions="FillAndExpand" CancelButtonColor="Red" SearchButtonPressed="SearchTextBox_SearchButtonPressed" Placeholder="Search for Destination"></SearchBar>

                    <ActivityIndicator  IsVisible="False" x:Name="MyActivityIndicator" Color="#039763" IsRunning="False"></ActivityIndicator>
                </StackLayout>
            </StackLayout>

            <StackLayout x:Name="mystacklayout" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
             
            </StackLayout>

            <StackLayout BackgroundColor="White" Orientation="Vertical" HorizontalOptions="FillAndExpand">
                <StackLayout BackgroundColor="White" Orientation="Vertical" HorizontalOptions="FillAndExpand" Padding="10">
                    <synbtn:SfButton x:Name="cmdGetCoordinateDestination" CornerRadius="7" BackgroundColor="#E66C2C" BorderColor="#039763" HasShadow="False"  TextColor="White" FontSize="14" HeightRequest="42" Text="Get GPS Coordinates" Clicked="cmdGetCoordinateDestination_Clicked"></synbtn:SfButton>
                    <synbtn:SfButton x:Name="cmdSUBMIT" CornerRadius="7" IsVisible="False" IsEnabled="True" BorderColor="LightGray" HasShadow="False" BackgroundColor="#039763" TextColor="White" FontSize="16" HeightRequest="45" Text="SUBMIT REPORT" Clicked="CreateAccountClicked"></synbtn:SfButton>

                    <!--<Label x:Name="lbldistance" Text="Within 50 Kilometers (KM)" HorizontalTextAlignment="Center" TextColor="Black" FontSize="11" />
                <syncslider:SfRangeSlider x:Name="theSlider" HeightRequest="60" ShowRange="False" SnapsTo="StepValues" StepFrequency="20" Orientation="Horizontal" HorizontalOptions="CenterAndExpand" TickPlacement="Inline" ValueChangeMode="Default" Minimum="0" Maximum="300" Value="50" ValueChanging="theSlider_ValueChanging"></syncslider:SfRangeSlider>-->
                </StackLayout>
            </StackLayout>

        </StackLayout>

        <StackLayout x:Name="mycentremarker_layout" IsVisible="True" Padding="12"
             AbsoluteLayout.LayoutFlags="PositionProportional"
             AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1">

        </StackLayout>             
    </AbsoluteLayout>

现在,将其与一些 C# 代码相结合,用于向页面添加和初始化 Xamarin.Forms.Map 和中心标记图标

private async void PrepareMap()
    {
        try
        { 
            var request = new GeolocationRequest(GeolocationAccuracy.Medium);
            var location = await Geolocation.GetLocationAsync(request).ConfigureAwait(true);

            if (location != null)
            {
                App.lastlatitude = location.Latitude; //double.Parse("8.13463");
                App.lastlongitude = location.Longitude; //double.Parse("-13.30254");
            }
        
            if (App.lastlatitude != 0)
            {
                var position = new Position(App.lastlatitude, App.lastlongitude);

                customMap = new CustomMap
                {
                    MapType = MapType.Street,
                    IsShowingUser = true,
                    WidthRequest = 100,
                    HeightRequest = 100,
                    VerticalOptions = LayoutOptions.FillAndExpand
                };

                customMap.MapType = MapType.Street;
                customMap.TrafficEnabled = true;

                if (mystacklayout.Children.Any())
                {
                    mystacklayout.Children.Clear();
                }

                mystacklayout.Children.Add(customMap);

                    customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(App.lastlatitude, App.lastlongitude), Distance.FromMiles(5)));
            
                customMap.IsShowingUser = true;

                TheCoordinates = App.lastlatitude + ", " + App.lastlongitude;

                var thelatdecimal = App.lastlatitude.ToString().Split('.')[1];
                var thelongdecimal = App.lastlongitude.ToString().Split('.')[1];

                if (thelatdecimal.Length > 5)
                {
                    thelatdecimal = thelatdecimal.Substring(0, 5);
                }

                if (thelongdecimal.Length > 5)
                {
                    thelongdecimal = thelongdecimal.Substring(0, 5);
                }

                var thelat = App.lastlatitude.ToString().Split('.')[0] + "." + thelatdecimal;
                var thelong = App.lastlongitude.ToString().Split('.')[0] + "." + thelongdecimal;

                lblCoordinates.Text = thelat + ", " + thelong;

                AddTheCentreMarker();
            }
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", ex.Message, "OK").ConfigureAwait(true);
        }
    }

    private async void AddTheCentreMarker()
    {
        try
        {

            Image _imgMarker = new Image();  //marker holder declaration
            int int_markerSize; //marker sizer 
            _imgMarker.Source = Device.OnPlatform(
                iOS: ImageSource.FromUri(new Uri("http://www.launchappy.com/images/Marker.png")),
                Android: ImageSource.FromFile("Marker.png"),
                WinPhone: ImageSource.FromFile("Marker.png"));
            _imgMarker.VerticalOptions = LayoutOptions.CenterAndExpand;
            int_markerSize = 20;
            _imgMarker.WidthRequest = int_markerSize;
            _imgMarker.HeightRequest = int_markerSize;

            mycentremarker_layout.Children.Add(_imgMarker);
        }
        catch (Exception ex)
        {
            throw;
        }
    } 

希望这有助于解决问题