Adding a Pushpin on MapControl Tap event

I'm trying to add a Pushpin to my MapControl on Tap. The problem I am facing is, the GestureEventArgs passes a Map relative co-ordinate for eg:

X = 216
Y = 197

Due to this I am not able to assign the Co-ods on a Pushpin to Add into the map. Here's exactly what I am doing.

private void MainMap_Tap(object sender, GestureEventArgs e)
    {
        Point p = e.GetPosition(this.MainMap);
        GeoCoordinate g = new GeoCoordinate();
        g = MainMap.ViewportPointToLocation(p);

        MyPin.Location = g;
        MainMap.Children.Add(MyPin);
    }

My code breaks due to a Null Reference Error at MyPin.Location = g. In g I get values like

g = {-0.00446319579627641, 0.00369071960449219}

Answers


Probably because your MyPin is not initialized.

You cannot set the Location of a Pushpin if it's null.

http://msdn.microsoft.com/en-us/library/gg588563(v=vs.92)


Need Your Help

ASP.NET MVC 2 Problem with custom ModelValidator

c# .net asp.net asp.net-mvc

I am trying to validate that either of 2 textbox fields in my view have a value supplied. I made this Model Validator: