Search code examples
c#dictionarytoolkitmarkers

How to add a PushPin item to a map? Windows Phone


I'm trying to draw a pushpin to a map in the map page of my application but when I add the code to overlay a pushpin to the map, I get the following errors:

The type or namespace name PushPin could not be found (are you missing a using directive or an assembly reference?)

and

Microsoft.Phone.Maps.Controls.Map does not contain a definition for Children and no extension method Children accepting a first argument of type Microsoft.Phone.Maps.Controls.Map could be found

I understand from this that I'm missing a reference? I have references for controls.maps and contols.toolkit, so I can't understand why.

Also this is the code I'm using to draw the pushpin:

PushPin myPin = new Pushpin();
myPin.Location = MyGeoPosition;
myPin.Content = "My car";
MyMap.Children.Add(myPin);

Solution

  • Try this.

    MapLayer layer1 = new MapLayer();
    
    Pushpin pushpin1 = new Pushpin();
    
    pushpin1.GeoCoordinate = MyGeoPosition;
    pushpin1.Content = "My car";
    MapOverlay overlay1 = new MapOverlay();
    overlay1.Content = pushpin1;
    overlay1.GeoCoordinate = MyGeoPosition;
    layer1.Add(overlay1);
    
    myMap.Layers.Add(layer1);
    

    You create new overlay for each pushpin, add all overlays to a layer, and add the layer to the map element.