问题
I have a method to draw pushpins to a map which works fine, but I need to append a second pushpin to the first layer. I'm guessing this will involve saving the first layer so that it can be manipulated later. Does anyone have any idea how I would go about this as I'm not quite sure how to save a layer?
At present the DrawPushPin method is called twice in the app, so the first time a new layer is created and same for the second time but this is not ideal as I need to append to the first layer not create a new one.
The method is called like this DrawPushPinCurrent(MyGeoPosition, pushPinName);
and below is the draw method.
private void DrawPushPin(GeoCoordinate MyGeoPosition,string pushPinName)
{
MapLayer layer1 = new MapLayer();
Pushpin pushpin1 = new Pushpin();
pushpin1.GeoCoordinate = MyGeoPosition;
pushpin1.Content = pushPinName;
MapOverlay overlay1 = new MapOverlay();
overlay1.Content = pushpin1;
overlay1.GeoCoordinate = MyGeoPosition;
layer1.Add(overlay1);
MyMap.Layers.Add(layer1);
MyMap.Center = MyGeoPosition;
MyMap.ZoomLevel = 15;
}
回答1:
What I would do is move the MapLayer outside of the method and make them global (at the class level). Then I would create another method like this:
MapLayer layer1;
public MyClass()
{
layer1 = new MapLayer;
}
private void AppendPushpin(GeoCoordinate MyGeoPosition, string pushpinName)
{
Pushpin pushpin1 = new Pushpin();
pushpin1.GeoCoordinate = MyGeoPosition;
pushpin1.Content = pushPinName;
MapOverlay overlay1 = new MapOverlay();
overlay1.Content = pushpin1;
overlay1.GeoCoordinate = MyGeoPosition;
layer1.Add(overlay1);
}
*EDIT
In your case, where you need to store that variable application-wide, you would want to persist that MapLayer object doing something like this:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["MapLayer"] = layer1;
Its up to you on where you need to set and get this global variable.
来源:https://stackoverflow.com/questions/22747011/how-to-save-and-append-to-a-map-layer