How to give image to a pushpin dynamically in c# - windows-phone-7

I have a scenario where I would like the user to view multiple pushpins in Bing maps in a wp7 app. I used maplayer to make the cluster of pushpins, but I could not add an image to that pushpin dynamically in the cs file itself. By the way, I have not used the pushpin control in xaml. I'm just adding the pushpin object to the maplayer while looping through.
Here is my code:
maplayer layer = new maplayer();
watcher.start();
for (int i = 0; i < lst.count; i++)
{
Pushpin mypin = new Pushpin();
watcher.Position.Location.Latitude = Convert.ToDouble(lst[i].Latitude);
watcher.Position.Location.Longitude=Convert.ToDouble(lst[i].Longitude);
}
GeoCoordinate geo = new GeoCoordinate(watcher.Position.Location.Latitude, watcher.Position.Location.Longitude);
mypin.Location = geo;
mypin.Background = new SolidColorBrush(Colors.Gray);
mypin.Foreground = new SolidColorBrush(Colors.White);
mypin.Content = "My location";
layer.AddChild(mypin, mypin.Location);
}
map1.SetView(watcher.Position.Location, Status == true ? 5.0 : 3.0);
map1.Children.Add(layer);
watcher.stop();
I have also tried using the image brush property to provide image source to pushpin but the pushpin itself goes invisible.
like this:
ImageBrush ib = new ImageBrush();
ib.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(#"Images/push.png", UriKind.Relative));
mypin.Background = ib;
Please help me on this. I need this to be done without changing/adding datatemplate to pushpin from the xaml side.

This problem is covered in MSDN, on the page for Working With Pushpins. Here is the example given, where images are added directly to a layer on the map:
namespace WindowsPhoneApplication1
{
public partial class MainPage : PhoneApplicationPage
{
MapLayer imageLayer;
public MainPage()
{
InitializeComponent();
//Create a layer to contain the pushpin images.
imageLayer = new MapLayer();
map1.Children.Add(imageLayer);
}
private GeoCoordinate mapCenter;
private void button1_Click(object sender, RoutedEventArgs e)
{
// Retrieve the center of the current map view.
mapCenter = map1.Center;
// Define the image to use as the pushpin icon.
Image pinImage = new Image();
//Define the URI location of the image.
pinImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("bluepushpin.png", UriKind.Relative));
//Define the image display properties.
pinImage.Opacity = 0.8;
pinImage.Stretch = System.Windows.Media.Stretch.None;
// Put the image at the center of the view.
PositionOrigin position = PositionOrigin.Center;
imageLayer.AddChild(pinImage, mapCenter, position);
}
}
}

Related

How to load image onto a button in xamarin forms PCL?

I am picking a photo from photo library and i get the following
AlbumPath:
assets-library://asset/asset.JPG?id=106E99A1-4F6A-45A2-B320-B0AD4A8E8473&ext=JPG
Path:
/Users/myname/Library/Developer/CoreSimulator/Devices/21CB035B-A738-4F74-B121-2DB2A6B5372A/data/Containers/Data/Application/3081A323-98AF-4EF6-95B9-29D4C2CD8425/Documents/temp/IMG_20170408_111143.jpg
How do i assign this image to a button ? I tried the following.
var file = await CrossMedia.Current.PickPhotoAsync(
new Plugin.Media.Abstractions.PickMediaOptions
{
});
Button btn = new Button();
btn.Image = (Xamarin.Forms.FileImageSource)ImageSource.FromFile(file.Path);
No image is displayed on the button. Any help help is appreciated.
Thank you.
Once you deploy the application the app will not have access to your mac. For the application to use the picture it will need to be a bundled resource within the iOS application. Below is a simple example of using images within an iOS app. You'll definitely want to consider just adding a gesture listener to your image instead of making it a button though. If you try to just use the image on a button you'll have to do some styling adjustments to get it to look clean.
Image Setup
Put image in iOS Resource folder
Make sure bundledresource is selected from image properties.
Image Button
public class ImageButton : ContentPage
{
private Button _imageBtn = new Button { Image = "icon.png", Text = "Sample button" };
public ImageButton()
{
Content = new StackLayout
{
Children =
{
_imageBtn
}
};
}
}
Image with TapGesture
public class ImageButton : ContentPage
{
private Image _imageBtn = new Image { Source = "icon.png" };
private TapGestureRecognizer _imageTap = new TapGestureRecognizer();
public ImageButton()
{
_imageBtn.GestureRecognizers.Add(_imageTap);
Content = new StackLayout
{
Children =
{
_imageBtn
}
};
_imageTap.Tapped += (s, e) =>
{
// handle the tap
};
}
}

Background image with Carousel effect

I would like to create a layout with a fullscreen background image and some UI elements on top of it. The twist is this:
I would like the background image to swipeable like a carousel, but I would like the UI elements to stay in place. That is if I swipe the screen, the background image should slide to the side and a new image should replace it. I know about CarouselPage, but it seems to me that it won't do the trick, since a Page can have only one child which it replaces on swipe, meaning that the UI elements would be descendants of the CarouselPage and therefore would also be animated.
I am guessing I need some sort of custom renderer here, but how should I go about designing it? Should it be one fullscreen Image control replaced be another fullscreen Image control with the UI elements on top of it? And how can I do this? Or is there an all together better approach?
I am developing for iOS and Android using Xamarin.Forms.
Thanks in advance.
I don't like repeating myself much, and I think that multiple layers of actionable items can lead to confusion, but the problems appeals to me and I can see a niche for this kind of UI, so here's my take on your question.
Let's assume this is the (Xamarin.Forms.)Page you want to render with a custom carousel background:
public class FunkyPage : ContentPage
{
public IList<string> ImagePaths { get; set; }
public FunkyPage ()
{
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Spacing = 12,
Children = {
new Label { Text = "Foo" },
new Label { Text = "Bar" },
new Label { Text = "Baz" },
new Label { Text = "Qux" },
}
};
ImagePaths = new List<string> { "red.png", "green.png", "blue.png", "orange.png" };
}
}
The renderer for iOS could look like this:
[assembly: ExportRenderer (typeof (FunkyPage), typeof (FunkyPageRenderer))]
public class FunkyPageRenderer : PageRenderer
{
UIScrollView bgCarousel = new UIScrollView (RectangleF.Empty) {
PagingEnabled = true,
ScrollEnabled=true
};
List<UIImageView> uiimages = new List<UIImageView> ();
protected override void OnElementChanged (VisualElementChangedEventArgs e)
{
foreach (var sub in uiimages)
sub.RemoveFromSuperview ();
uiimages.Clear ();
if (e.NewElement != null) {
var page = e.NewElement as FunkyPage;
foreach (var image in page.ImagePaths) {
var uiimage = new UIImageView (new UIImage (image));
bgCarousel.Add (uiimage);
uiimages.Add (uiimage);
}
}
base.OnElementChanged (e);
}
public override void ViewDidLoad ()
{
Add (bgCarousel);
base.ViewDidLoad ();
}
public override void ViewWillLayoutSubviews ()
{
base.ViewWillLayoutSubviews ();
bgCarousel.Frame = View.Frame;
var origin = 0f;
foreach (var image in uiimages) {
image.Frame = new RectangleF (origin, 0, View.Frame.Width, View.Frame.Height);
origin += View.Frame.Width;
}
bgCarousel.ContentSize = new SizeF (origin, View.Frame.Height);
}
}
This was tested and works. Adding a UIPageControl (the dots) is easy on top of this. Autoscrolling of the background is trivial too.
The process is similar on Android, the overrides are a bit different.

how to add tooltip to pushpin on windowsphone

I've been doing a essay about map on windows phone. I added pushpins default into map and its content is a image. I want to show information when I tap or click on pushpin, but I don't know what to do. I've just thought about use tooltip to show info, but I can't do it.
here is my createpushpin function.
can you help me? thank you for all kind of you!
private void CreateNewPushpin(object selectedItem)
{
var pushpinPrototype = selectedItem as Pushpinsmodel;
var pushpinicon = pushpinPrototype.Icon;
Pushpin pushpins = new Pushpin() { Tag = adress.Name };
pushpins.Background = new SolidColorBrush(Colors.Green);
pushpins.Location = new GeoCoordinate(lad, long);
ImageBrush image = new ImageBrush() {
ImageSource = new System.Windows.Media.Imaging.BitmapImage
(pushpinicon)};
Ellipse elip = new Ellipse()
{
Fill = image,
Name = adress.Name,
StrokeThickness = 10,
Height = 30,
Width = 30
};
pushpins.Content = elip;
var tooltipText = new ToolTip { Content = adress.Name};
ToolTipService.SetToolTip(pushpins, tooltipText);
map1.Children.Add(pushpins);
listpushpin.Add(pushpins);
this.map1.SetView(pushpins.Location, 18.0);
}

How to add Info Box in Bing map Controll?

I have a Bing map Controll on my form and this bing map contains so many pushpins.
When I click on pushpin at that time i want to show an info box.
So how can I add this info box?
please see bellow image.i have to requery this type of info box.
Here is a sample code. All you have to do is attach a Tap event to the pushpin
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
InitPage();
}
private void InitPage()
{
testMap.Center = new GeoCoordinate(42,42);
testMap.ZoomLevel = 10;
MapLayer pushPinLayer = new MapLayer();
testMap.Children.Add(pushPinLayer);
Pushpin pushpin = new Pushpin();
pushpin.Tap += new EventHandler<GestureEventArgs>(pushpin_Tap);
pushpin.Content = "SAMPLE";
pushPinLayer.AddChild(pushpin, new GeoCoordinate(42, 42));
}
void pushpin_Tap(object sender, GestureEventArgs e)
{
MessageBox.Show("You Clicked here");
}
}
yes i got what are you saying
you just put image button over all the geocoordination
from that you replace the pushpin in place of image buton when any button is press
and dynamically change the content of pushpin in every points

Passing values between 2 pages in Wp7 havig scrollviewer

I have 2 pages in my app each page has one scrollviewer having image as an item added dynamically form URL. The first contain the first image.When I scroll to the end of first page the second page have to appear with the next image, viceversa when I scroll to the end of the second page now the first page should contain the third image and next the second with the 4th image. how to do that. It is like an ebook application.
This is in firstpage.xaml.cs
Image img = new Image();
Uri uri = new Uri("http://d1mu9ule1cy7bp.cloudfront.net/2012/media/catalogues/47/pages/p_" + i + "/high.jpg");
ImageSource img1 = new BitmapImage(uri);
img.Source = img1;
img.Height = 550;
Scrollview.Content = img;
private void Scrollview_MouseEnter(object sender, MouseEventArgs e)
{
this.NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
}
This is in secondpage.xaml.cs
int h = loop(j);
Image img = new Image();
Uri uri = new Uri("http://d1mu9ule1cy7bp.cloudfront.net/2012/media/catalogues/47/pages/p_" + h + "/high.jpg");
ImageSource img1 = new BitmapImage(uri);
img.Source = img1;
img.Height = 550;
Scrollview.Content = img;
private void Scrollview_MouseEnter(object sender, MouseEventArgs e)
{
this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
}
I would consider using a ListBox instead. It is a very powerful control.
I would recommend you to detect the end of scroll position on the first page and add the second image dynamically to that scollviewer itself. Something like infinite scrolling, similar to that of news feed in facebook app for wp7.

Resources