问题
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 forChildren
and no extension methodChildren
accepting a first argument of typeMicrosoft.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);
回答1:
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.
回答2:
You can try..
BitmapImage myImage1; myImage1 = new BitmapImage(new Uri("/Assets/Images/pushpin-google-hi.png", UriKind.RelativeOrAbsolute));
var image = new Image();
image.Width = 50;
image.Height = 50;
image.Opacity = 100;
image.Source = myImage1;
var mapOverlay = new MapOverlay();
mapOverlay.Content = image;
mapOverlay.GeoCoordinate = new GeoCoordinate(lats, lons);
var mapLayer = new MapLayer();
mapLayer.Add(mapOverlay);
MyMap.Layers.Add(mapLayer);
MyMap.SetView(new GeoCoordinate(lats, lons), 16);
回答3:
Try this. First I have this in a utils class:
public MapLayer GetCurrentPosition (out MapOverlay myPositionOverlay)
{
MapLayer myPositionLayer = new MapLayer();
myPositionOverlay = new MapOverlay();
Image myPositionImage = new Image();
myPositionImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Assets/myImagePin.png", UriKind.RelativeOrAbsolute));
myPositionImage.Height = 70;
myPositionImage.Visibility = Visibility.Visible;
myPositionImage.HorizontalAlignment = HorizontalAlignment.Center;
myPositionOverlay.Content = myPositionImage;
myPositionLayer.Add(myPositionOverlay);
return myPositionLayer;
}
Then this goes on your MapLoaded method:
MapLayer mypositionArrow = placePushPins.GetCurrentPosition(out myPositionOverlay);
MyMap.Layers.Add(mypositionArrow);
Then, if you want your pushpin not to be static, to move according to your position, add this to your watcher:
myPositionOverlay.GeoCoordinate = myCurrentPositionCoordinate;
To make the pushpin move smoothly just implement animation like this: http://blogs.bing.com/maps/2014/09/25/part-2-bring-your-maps-to-life-creating-animations-with-bing-maps-net/
来源:https://stackoverflow.com/questions/22364188/how-to-add-a-pushpin-item-to-a-map-windows-phone