开发者

Phone 7 Bing map control - Add a pushpin when tap

开发者 https://www.devze.com 2023-01-19 14:19 出处:网络
I am using the latest Phone 7 RTM tools ( downloaded it today, October 7 2010). I am trying to do a simple thing here:

I am using the latest Phone 7 RTM tools ( downloaded it today, October 7 2010).

I am trying to do a simple thing here:

when the user taps once on the map control, i want to put a pushpin there. also, i want to keep the regular built-in behavior of the map control ( tap twice to zoom).

(If it's not possible to keep both behaviors , then maybe a long press on the map to put pushpin).

When trying figuring this out, i came across this documentation of the changes made to the control map for Phone7: http://msdn.microsoft.com/en-us/library/ff955762.aspx

Then i saw the new class MapInputEventArgs, which has a ViewportPoint member.

When looking at code examples on the regular SilverLight map control i saw something like this:

private void OnMouseClick(object sender, MapMouseEventArgs e)
    {
        Point clickLocation = e.ViewportPoint;
        Location location = x_Map.ViewportPointToLocation(clickLocation);

        Pushpin pushpin = new Pushpin(); 
        m_P开发者_如何学CushpinLayer.AddChild(pushpin, new Location(latitude, longitude));
    }

But in Phone7 case, I can't find the appropriate event handler, and I could not find who uses MapInputEventArgs in the map control. Searching it on google gets me only 1 result !!

So , where is the appropriate event for "Tap once", and how can i get a ViewportPoint after this event has been fired ?

Thanks in advance.


Just figured this out if you are still having problems.

The MouseLeftButtonUp and MouseLeftButtonDown Events have a GetPosition Method that will return the point your looking for

 private void MapMain_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {

        Point p = e.GetPosition(this.MapMain);
        GeoCoordinate geo = new GeoCoordinate();
        geo = MapMain.ViewportPointToLocation(p);
        MapMain.ZoomLevel = 17;
        MapMain.Center = geo;
        //---create a new pushpin---
        Pushpin pin = new Pushpin();

        //---set the location for the pushpin---
        pin.Location = geo;

        //---add the pushpin to the map---
        MapMain.Children.Add(pin);
    }


Unless I'm reading your question wrong, this seems to be exactly what you're looking for:

Silverlight - Add Pushpin to Bing Maps via C#


Okay, it's not pretty, but I have the same problem and I came up with a workaround of sorts that kicks in when you release your fingers from the screen. I instantiate a boolean:

         bool noPin = false;

I then use this to determine whether zoom or pan is being done by the user (these fire between the MouseLeftButtonDown and MouseLeftButtonUp events). On the Up event, I then check whether the user was zooming or panning and, if not, place my pushpin.

    private void mHome_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        noPin = false;
    }

    private void mHome_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (!noPin)
            PlacePushPin();
    }

    private void mHome_MapPan(object sender, MapDragEventArgs e)
    {
        tbTemp.Text += "pan";
    }

    private void mHome_MapZoom(object sender, MapZoomEventArgs e)
    {
        tbTemp.Text += "zoom";
    }

It's not beautiful but, well, it was the best I could manage.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号