![](/img/trans.png)
[英]In C#, how do I get a nearest place from Bing Maps from a longitude and latitude value?
[英]How to set the longitude and latitude from ViewModel in bing maps in windows phone 7
我想從ViewModel中給出經度和緯度。
現在我使用像:
private void button1_Click(object sender, RoutedEventArgs e)
{
Pushpin p = new Pushpin();
p.Background = new SolidColorBrush(Colors.Yellow);
p.Foreground = new SolidColorBrush(Colors.Black);
p.Location = new GeoCoordinate(double.Parse(longitude.Text), double.Parse(latitude.Text));//Longitude and Latitude
p.Content = "I'm here";//To show the place where it is located
map1.Children.Add(p);
map1.SetView(new GeoCoordinate(double.Parse(longitude.Text), double.Parse(latitude.Text), 200), 9);
}
我的Xaml是:-
<Grid x:Name="MapPageUIContainer" Grid.Row="1" Margin="2,0,2,0">
<my:Map CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" CredentialsProvider="" Mode="AerialWithLabels" Height="543" HorizontalAlignment="Left" Name="map1" VerticalAlignment="Top" Width="480" Margin="2,100,0,0" />
<Border BorderBrush="Silver" BorderThickness="1" Height="100" HorizontalAlignment="Left" Margin="0,0,0,0" Name="border1" VerticalAlignment="Top" Width="476" Background="#FFA3A371">
<TextBlock Text="Map Example" HorizontalAlignment="Center" FontSize="32" FontWeight="Bold" VerticalAlignment="Center" />
</Border>
<TextBox Height="72" HorizontalAlignment="Left" Margin="6,627,0,0" Name="longitude" Text="" VerticalAlignment="Top" Width="200" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="260,627,0,0" Name="latitude" Text="" VerticalAlignment="Top" Width="200" />
<Button Content="Set" HorizontalAlignment="Left" Margin="190,690,0,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" />
</Grid>
在這里,我想從視圖模型中給出圖釘,經度,緯度。 請讓我知道如何實現此目標?
提前致謝..
我有這樣的嘗試。
public class MapPageViewModel : ReactiveObject
{
public static string _longitude;
public string longitude
{
get { return _longitude; }
set { this.RaiseAndSetIfChanged(x => x.longitude, value); }
}
public static string _latitude;
public string latitude
{
get { return _latitude; }
set { this.RaiseAndSetIfChanged(x => x.latitude, value); }
}
public ReactiveAsyncCommand setButton { get; set; }
public MapPageViewModel()
{
setButton = new ReactiveAsyncCommand();
setButton.Subscribe(x => {
Pushpin p = new Pushpin();
p.Background = new SolidColorBrush(Colors.Yellow);
p.Foreground = new SolidColorBrush(Colors.Black);
p.Location = new GeoCoordinate(double.Parse(longitude), double.Parse(latitude));
p.Content = "I'm here";//To show the place where it is located
//map1.Children.Add(p);
//map1.SetView(new GeoCoordinate(double.Parse(longitude), double.Parse(latitude), 200), 9);
});
}
}
但是我不知道如何設置map1.Children.Add()和map1.SetView以及如何在Map中綁定這些值?
嗨克萊門斯。 我已經嘗試過您的指示。 但是顯示錯誤。
而且我也嘗試過這個:-
public MapPageViewModel()
{
PushpinItems = new ObservableCollection<PushpinItem>();
PushpinItem pp = new PushpinItem();
setButton = new ReactiveAsyncCommand();
setButton.Subscribe(x => {
pp.Location = new GeoCoordinate(double.Parse(longitude), double.Parse(latitude));
pp.Text = "I'm here";
PushpinItems.Add(pp);
});
}
但是這里發生了運行時錯誤。 請讓我知道我在哪里弄錯了。
在適當的MVVM方法中,通常會具有一個MapItemsControl
和圖釘的ItemTemplate
,並將其綁定到視圖模型中的圖釘數據項的ObservableCollection
:
public class PushpinItem
{
public GeoCoordinate Location { get; set; }
public string Text { get; set; }
}
public class MapPageViewModel : ReactiveObject
{
public ObservableCollection<PushpinItem> PushpinItems { get; set; }
...
public MapPageViewModel()
{
PushpinItems = new ObservableCollection<PushpinItem>();
setButton = new ReactiveAsyncCommand();
setButton.Subscribe(x =>
{
PushpinItems.Add(new PushpinItem
{
Location = new GeoCoordinate(...),
Text = ...
});
});
}
}
XAML:
<map:Map ...>
<map:MapItemsControl ItemsSource="{Binding PushpinItems}">
<map:MapItemsControl.ItemTemplate>
<DataTemplate>
<map:Pushpin Location="{Binding Location}" Content="{Binding Text}"
Background="Yellow" Foreground="Black"/>
</DataTemplate>
</map:MapItemsControl.ItemTemplate>
</map:MapItemsControl>
</map:Map>
SetView從ViewModel @Clemens和此鏈接對我非常有幫助.. !! 謝謝他們兩個.. !!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.