Pregunta

Possible Duplicate:
How can I add programmatically add a PushPin, and could I make it have a custom image?

In WP7 using bing maps control, I used an image as a pushpin, but it was slightly offset due to how it renders the pushpin, so I used the PositionOrigin to center it properly like this:

 <my:Pushpin Location="{Binding Location}" 
                                    PositionOrigin="Center"MouseLeftButtonUp="pin_click"
                                    Template="{StaticResource PushpinControlTemplate1}" >
                    </my:Pushpin>

In The new Map control for WP8, i try and sung this setting, and the app just crashes to an unhandled exception in App.xaml.cs with no information.

This is the windows Phone 8 code:

  toolkit:Pushpin PositionOrigin="Center"  MouseLeftButtonUp="pin_click" 
GeoCoordinate="{Binding Location1}"  Template="{StaticResource PushpinControlTemplate1}"/

They both use this template:

 <ControlTemplate x:Key="PushpinControlTemplate1" TargetType="my:Pushpin">
        <Grid>
       <Image Width="45" Height="45" Source="{Binding Arrow}"/>
        </Grid>
    </ControlTemplate>

Any ideas how to offset or centre this pushpin?

EDIT: Example images:


(source: goapr.co.uk)

The left image with the standard pushpins shows the tip matches the transit stop on the map (as it should). My custom images should be on top of the transit stops on the right image, but they are not. The PositionOrigin setting used to almost fix this, but not possible in WP8 it seems.

¿Fue útil?

Solución

PushPins are aligned so their bottom-left point is at the GeoCoordinate. That's done by the default of of their MapOverlay PositionOrigin being set to 0,1. If you set the MapOverlay PositionOrigin to 0.5,0.5 MapOverlay will align it's center towards the GeoCoordinate. For example, UserLocationMarker uses PositionOrigin of 0.5,0.5 to align it's visual center to the coordinate.

Try playing around with the PushPin.PositionOrigin and see if that solves your problem. PushPin.PositionOrigin of 0.5,0.5 would be a good starting value depending on the effect you're trying to achieve. Remember, PushPin and UserLocationMarker are just fancy MapOverlay so when in doubt revert to plain MapOverlays to test stuff out.

<!-- Default Style used for Pushpin -->
<Style TargetType="maptk:Pushpin">
    <Setter Property="PositionOrigin" Value="0,1" />
</Style>

<!-- Default Style used for MePOI -->
<Style TargetType="maptk:UserLocationMarker">
    <Setter Property="PositionOrigin" Value="0.5,0.5" />
</Style>

One reason why you might have gotten exceptions when upgrading from WP7.5 to WP8 is because PositionOrigin has changed from Bing Maps custom data type to a simplified Point X,Y structure.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top