How to add a PushPin item to a map? Windows Phone - c#

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);

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.

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);

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/

Related

windows phone 8.1 bing maps clustering

Hello i am using windows map control described in this link and i have a MapLayer with multiple MapOverlays with differents pois on map. And i want to do the clustering thing. I try to do this but no ClusteringLayer exist and no Pushpin. How can i do the clustering?
var cluster = new ClusteringLayer();
layer = new ClusteringLayer(Mymap)
{
ClusterRadius = 10,
ClusterType = ClusteringType.Grid
};
//Add event handlers to create the pushpins
layer.CreateClusteredItemPushpin += CreateClusteredItemPushpin1;
layer.CreateItemPushpin+=layer_CreateItemPushpin;
private MapOverlay layer_CreateItemPushpin(object item, ClusteredPoint clusterInfo)
{
var x = clusterInfo.Location;
var poi = new BuildingPoi { Coordinate = x, Buid = _selectedBuild };
var imagePoiLocation = new Image
{
Source = new BitmapImage(new Uri("/Assets/MapPin.png", UriKind.Relative)),
DataContext = poi
};
var over = new MapOverlay();
imagePoiLocation.Tap += loadClickedBuilding;
over.Content = imagePoiLocation;
over.PositionOrigin = new Point(0.5, 0.5);
over.GeoCoordinate = new GeoCoordinate(x.Latitude, x.Longitude);
return over;
}
private MapOverlay CreateClusteredItemPushpin1(ClusteredPoint clusterInfo)
{
var x = clusterInfo.Location;
var poi = new BuildingPoi { Coordinate = x, Buid = _selectedBuild };
var imagePoiLocation = new Image
{
Source = new BitmapImage(new Uri("/Assets/MapPin.png", UriKind.Relative)),
DataContext = poi
};
var over = new MapOverlay();
imagePoiLocation.Tap += loadClickedBuilding;
over.Content = imagePoiLocation;
over.PositionOrigin = new Point(0.5, 0.5);
over.GeoCoordinate = new GeoCoordinate(x.Latitude, x.Longitude);
return over;
}
As they don't have any nuget package or dll to reference directly, you need to download the source code of specific classes like ClusteringLayer & PushPin with related .cs files or the project itself to your machine and add reference of this project in your windows phone project to get ClusteringLayer and PushPin classes.
See following screenshot for ClusteringLayer class. For other classes, just import the solution to visual studio and you will see source code of all the classes. BTW, ClusteringLayer constructor need at-least one argument in cluster.
I advice you to download source code and get familiar with it's usage from samples in source code.

How to add a map marker to map? Windows Phone

I have passed a GeoCoordinate variable to the page of my map class, but when I try to draw a marker to the map I get an error stating that GeoCoordinate is a 'type' but is used as a 'variable' It also gives me syntax error for the ; at PositionOrigin = new Point(0.5, 0.5);
and the closing ;.
I understand from this that my syntax must be incorrect for adding a map marker due to getting this error. My question is how do I correct this method to draw the marker?
Am I drawing the marker the correct way or is there a different solution?
This is the onNaviagatedTo method of the map page where I pass the coordinates and try to add a marker:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("GeoLat") && NavigationContext.QueryString.ContainsKey("GeoLong"))
{
var latitude = Convert.ToDouble(NavigationContext.QueryString["GeoLat"]);
var longtitude = Convert.ToDouble(NavigationContext.QueryString["GeoLong"]);
var MyGeoPosition = new GeoCoordinate(latitude, longtitude);
var overlay = new MapOverlay
{
PositionOrigin = new Point(0.5, 0.5); //syntax error
GeoCoordinate = MyGeoPosition; //error thrown here
Content = new TextBlock{Text = "My car"};
var ml = new MapLayer { overlay };
MyMap.Layers.Add(ml);
}; //syntax error
}
Looks to me like you're missing an equal sign.
MapOverlay overlay = new MapOverlay()
{
PositionOrigin = new Point(0.5, 0.5),
GeoCoordinate = MyGeoPosition,
Content = new TextBlock{Text = "My car"},
};
MapLayer ml = new MapLayer { overlay };
MyMap.Layers.Add(ml);
Or, you could just do this with PushPins:
PushPin myPin = new Pushpin();
myPin.Location = MyGeoPosition;
myPin.Content = "My car";
MyMap.Children.Add(myPin);

Trying to add overlays to map error

I just cleared layers off my mapping program and when I try to add a new layer I receive this error message.
newSystem.ArgumentException was unhandled by user code
HResult=-2147024809
Message=Value does not fall within the expected range.
Source=System.Windows
InnerException:
If anyone knows why this is I would very much appreciate your assistance
private void loadZoomLevel12Pics()
{
map1.Layers.Clear();
MapLayer pinLayer = new MapLayer();
// Create a new empty Pushpin
// Beny Sur- Mer War Cemetary
MapOverlay pinOverlay = new MapOverlay();
// Add the location of the Pushpin using latitude and longitude.
pinOverlay.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
//Image pinOverlayImage = new Image();
pinOverlayImage.Source = new BitmapImage(new Uri("images/Hedgehog.png", UriKind.Relative));
pinOverlay.Content = pinOverlayImage;
pinOverlay.PositionOrigin = new Point(0.0, 0.0);
pinOverlayImage.Opacity = 0.8;
pinOverlayImage.Height = 8;
pinOverlayImage.Width = 8;
pinOverlayImage.Tap += pinOverlayImage_Tap;
pinLayer.Add(pinOverlay);
map1.Layers.Add(pinLayer);
Then these pictures are cleared and a new zoom level is loaded
private void loadZoomLevel13Pics()
{
map1.Layers.Clear();
MapLayer pinLayer = new MapLayer();
// Create a new empty Pushpin
// Beny Sur- Mer War Cemetary
MapOverlay pinOverlay = new MapOverlay();
// Add the location of the Pushpin using latitude and longitude.
pinOverlay.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
//Image pinOverlayImage = new Image();
pinOverlayImage.Source = new BitmapImage(new Uri("images/Hedgehog.png", UriKind.Relative));
pinOverlay.Content = pinOverlayImage;
pinOverlay.PositionOrigin = new Point(0.0, 0.0);
pinOverlayImage.Opacity = 0.8;
pinOverlayImage.Height = 30;
pinOverlayImage.Width = 30;
pinOverlayImage.Tap += pinOverlayImage_Tap;
pinLayer.Add(pinOverlay);
map1.Layers.Add(pinLayer); // THIS IS THE LINE CAUSING THE PROBLEM
All of the images are declared globally because they are used in other functions/methods inside the program.
It seems like its trying to add the same layer that was previously added and is having difficulty doing so, but all of the layers are cleared on the first line of the method.
I had this same issue using a global polygon instead of an image. The issue is that even though you are calling map1.Layers.Clear() this does not get done immediately after it is called therefore you need to create a new image. In general, this can be fixed by not using global/instance variables for the overlay.Content...just declare it each time and assign it to the Content.

How to retrieve the name of a Panorama-Item at runtime?

I am programmatically adding items to my Panorama Control called PanoramaCC.
//function to create the panorama items in our view
private void showPanorama(string panoramaName)
{
//create the panorama item and define it
PanoramaItem genItem = new PanoramaItem();
genItem.Height = 265;
genItem.Width = 440;
genItem.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(PanoramaItem_Tap);
genItem.Name = panoramaName;
//create the stackpanel for the panoramaitem
StackPanel genStack = new StackPanel();
genStack.Orientation = System.Windows.Controls.Orientation.Horizontal;
//margin to be done
genStack.Margin = new Thickness(0, -20, 0, 20);
//load the image
Image genImg = new Image();
genImg.Height = 220;
genImg.Width = 400;
genImg.Stretch = System.Windows.Media.Stretch.Fill;
genImg.Margin = new Thickness(20, 5, 20, 5);
string path = "Assets/AppGraphics/CreditCards/" + panoramaName.ToString() + "Front.png";
Uri uriR = new Uri(path, UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uriR);
genImg.Source = imgSource;
//add image into stackpanel
genStack.Children.Add(genImg);
//add stackpanel to the panoramaitem
genItem.Content = genStack;
//add the panoramaitem to the panoramaview
this.PanoramaCC.Items.Add(genItem);
}
The issue I have is that during runtime I want to retrieve the name of the panoramaItem I am currently looking at and do something with it. I've managed to retrieve the name through the tap event for navigation purposes, string name = ((PanoramaItem)sender).Name; but this is a diffrent scenario. I want to retrieve the name and then delete the item with the corresponding name. Pressing a button should delete the currently selected panoramaItem, is what I'm trying to achieve.
You can get the current PanoramaItem by using the SelectedItem property. You don't need to get the name to delete it.
PanoramaItem currentItem = myPanorama.SelectedItem as PanoramaItem;
if(currentItem != null)
{
//if you want the name for other reasons
string name = currentItem.Name;
//Items returns an ItemsCollection object
myPanorama.Items.Remove(currentItem);
}

WPF Setter Class

I want to create a button in c# with no content but just a image within it.
I dont know how to use the setter. Can anyone enlighten me?
Setter setter = new Setter();
setter.Property = Image.SourceProperty;
setter.Value = "asd.jpg"; //<--error
Style Rstyle = new Style();
Rstyle.TargetType = typeof(Button);
Rstyle.Setters.Add(setter);
Button _Button = new Button()
{
Width = 41,
Height = 41
};
_Button.Style = Rstyle;
What should i put at setter.Value to obtain the image which resides in the project directory. "asd.jpg"
Button has a Content property that you can just dump a BitmapImage in it:
BitmapImage image = new BitmapImage(new Uri("your source"));
TheButton.Content = image;
the setter is not used in c# code, its a helper class to build styles in xamls.
Just use this code.
BitmapImage bmp = new BitmapImage()
bmp.BeginInit();
bmp.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/myImage.png");
bmp.EndInit();
Image image = new Image();
image.Source = bmp;
myButton.Content = image;
<ControlTemplate x:Key="image"> //XAML in window resources tag
<Image Source="Save.png"/>
</ControlTemplate>
btn.Template = (ControlTemplate)FindResource("image");
I think this is more flexible and reusable.

Categories

Resources