Reputation: 509
I am developing a windows phone 8 application, i have to implement a map control with a pushpin which will show current location of the users, i am assigning current location to my pushpin, now i want this pushpin to be in the center of the map. Can anyone help me out in binding the pushpin to the center of map.
<maps:Map x:Name="MyMap" Center="{Binding}" ZoomLevel="15">
<toolkit:MapExtensions.Children>
<toolkit:Pushpin x:Name="pushpin1" GeoCoordinate="{Binding}">
<toolkit:Pushpin.Template>
<ControlTemplate TargetType="toolkit:Pushpin">
<StackPanel>
<ContentPresenter x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Left"></ContentPresenter>
<Path Data="M0,0L1,1L2,0L2,0L1,0L0,0Z"
Fill="#00AAFF"
Stretch="Fill"
Margin="-2,0"
Height="120"
Width="30"
Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Visibility, Mode=TwoWay}"
HorizontalAlignment="Left"
/>
</StackPanel>
</ControlTemplate>
</toolkit:Pushpin.Template>
</toolkit:Pushpin>
</toolkit:MapExtensions.Children>
</maps:Map>
Upvotes: 0
Views: 2551
Reputation: 1901
You want use Bing map in Windows Phone 8 App? I have trouble with old Bing Map control in WP8 app. I wrote about it windows phone 8 old map vs new map. Best solution for WP 8 is new Nokia Map control.
In XAML:
xmlns:nokiamap="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"
<nokiamap:Map Name="NokiaMap">
<toolkit:MapExtensions.Children>
<toolkit:Pushpin x:Name="MyPushpin"/>
</toolkit:MapExtensions.Children>
</nokiamap:Map>
//Init Pushpin
private Pushpin MyPushpin { get; set; }
ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(NokiaMap);
var pin = children.FirstOrDefault(x => x.GetType() == typeof(Pushpin)) as Pushpin;
MyPushpin = pin;
// Start geolocator
public void StartGeolocator()
{
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.MovementThreshold = 10;
geolocator.PositionChanged += geolocator_PositionChanged;
geolocator.StatusChanged += geolocator_StatusChanged;
}
}
private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
myGeoposition = new GeoCoordinate()
{
Latitude = args.Position.Coordinate.Latitude,
Longitude = args.Position.Coordinate.Longitude,
}
MyPushpin.GeoCoordinate = myGeoposition;
NokiaMap.SetView(myGeoposition, NokiaMap.ZoomLevel);
}
See your code Pushpin location binding in windows phone 8 app is not working
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(MyMap);
var pin = children.FirstOrDefault(x => x.GetType() == typeof(Pushpin)) as Pushpin;
pin.DataContext = args.Position.Coordinate;
//witout binding
//pin.GeoCoordinate = args.Position.Coordinate;
});
}
Upvotes: 2