我正在尝试在 Xamarin.Forms 上集成一个地图,当打开该地图时,一个固定标记放置在地图的中心。固定,我的意思是当地图被拖动时它不会移动,并且标记本身也是不可拖动的。有人可以指导我完成此操作,还是您有任何我可以阅读的参考资料?我已经显示了地图。
问问题
497 次
2 回答
1
不要在地图上添加标记,而是用视图覆盖地图。
于 2015-12-29T12:07:39.220 回答
0
我遇到了同样的问题,经过很长时间的搜索,我终于结合了一些羊皮纸和逻辑,得出了一个最终可行的解决方案。我在 Github 上发布了 Centered Marker 问题的完整解决方案,这是链接。
https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/486
但是出于冗余的目的(如果您由于某种原因找不到帖子),这是解决方案
<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;
}
}
希望这有助于解决问题
于 2022-02-24T22:02:19.470 回答