Map tap event / mouse down event in windows phone

2019-02-25 02:41发布

问题:

I am using the windows phone emulator. I wrote a very simple program: draw a marker on the map when the user tap the map once.

Then I used map_tap event, and get the tapped location as follows,

private void map_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    Point p = e.GetPosition(null);
    GeoCoordinate s = map.ConvertViewportPointToGeoCoordinate(p);

    Ellipse myCircle = new Ellipse();
    myCircle.Fill = new SolidColorBrush(Colors.Blue);
    myCircle.Height = 20;
    myCircle.Width = 20;
    myCircle.Opacity = 50;

    MapOverlay myLocationOverlay = new MapOverlay();
    myLocationOverlay.Content = myCircle;
    myLocationOverlay.PositionOrigin = new Point(0, 0);
    myLocationOverlay.GeoCoordinate = s;

    MapLayer myLocationLayer = new MapLayer();
    myLocationLayer.Add(myLocationOverlay);

    map.Layers.Add(myLocationLayer);
}

The problem is that, the point I get is not the point where the mouse (in the emulator it is a mouse but not a finger) clicked. It is some distance lower (about 50 pixels lower) than where I clicked.

So wherever I click in the emulator, the circle is drawn below where I clicked, it's some kind of weird.

Is there anything wrong with my code?

Thank you very much.

回答1:

The GestureEventArgs.GetPosition() method takes a parameter specifying what UIElement to get the coordinate relative to (see the MSDN documentation). So, try doing

Point p = e.GetPosition(map);

instead.