Last active
May 1, 2019 15:25
-
-
Save codercampos/7429936d2e9e440ed0f24063d5bb37da to your computer and use it in GitHub Desktop.
Xamarin.Forms Google Maps Plugin ItemTemplate Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using Xamarin.Forms; | |
using Xamarin.Forms.GoogleMaps; | |
namespace YourNamespace.Framework.Converters | |
{ | |
public class IconBitmapConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
if (!(value is string icon)) return null; | |
return BitmapDescriptorFactory.FromBundle(icon); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage | |
xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:google="clr-namespace:Xamarin.Forms.GoogleMaps;assembly=Xamarin.Forms.GoogleMaps" | |
xmlns:local="clr-namespace:ToolApp.Framework.Extensions;assembly=ToolApp" | |
xmlns:converters="clr-namespace:ToolApp.Framework.Converters;assembly=ToolApp" | |
Visual="Material" | |
Icon="icon_tab_map" | |
Title="{local:Translate MapPageTitle}" x:Class="ToolApp.Pages.MapPage"> | |
<ContentPage.Resources> | |
<ResourceDictionary> | |
<converters:IconBitmapConverter x:Key="IconBitmapConverter" /> | |
</ResourceDictionary> | |
</ContentPage.Resources> | |
<ContentPage.Content> | |
<StackLayout> | |
<google:Map MyLocationEnabled="True" ItemsSource="{Binding Items}"> | |
<google:Map.ItemTemplate> | |
<DataTemplate> | |
<google:Pin | |
Type="Place" | |
Position="{Binding Position}" | |
Label="{Binding Name}" | |
Address="{Binding FullAddress}" | |
Icon="{Binding Pin, Converter={StaticResource IconBitmapConverter}}"> | |
</google:Pin> | |
</DataTemplate> | |
</google:Map.ItemTemplate> | |
</google:Map> | |
</StackLayout> | |
</ContentPage.Content> | |
</ContentPage> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace YourNamespace.ViewModels | |
{ | |
public class MainViewModel | |
{ | |
private List<YourModel> _items; | |
public List<YourModel> Items | |
{ | |
get => _items; | |
set => SetProperty(ref _items, value); | |
} | |
public MainViewModel() | |
{ | |
Items = GenerateYourItemsHere(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Prism.Behaviors; | |
using Xamarin.Forms; | |
using Xamarin.Forms.GoogleMaps; | |
namespace YourNamespace.Framework.Behaviors | |
{ | |
public class MapPositionBehavior : BehaviorBase<Map> | |
{ | |
public static readonly BindableProperty SelectedLocationProperty = | |
BindableProperty.Create(nameof(SelectedLocation), typeof(Position?), typeof(MapPositionBehavior), default(Position?), propertyChanged: OnSelectedPositionPropertyChanged); | |
public Position? SelectedLocation | |
{ | |
get => (Position?) GetValue(SelectedLocationProperty); | |
set => SetValue(SelectedLocationProperty, value); | |
} | |
private static void OnSelectedPositionPropertyChanged(BindableObject bindableObject, object oldValue, | |
object newValue) | |
{ | |
if (bindableObject is MapPositionBehavior mapPositionBehavior) | |
{ | |
mapPositionBehavior.UpdateMap(); | |
} | |
} | |
protected override void OnAttachedTo(Map bindable) | |
{ | |
base.OnAttachedTo(bindable); | |
bindable.UiSettings.ScrollGesturesEnabled = false; | |
} | |
private void UpdateMap() | |
{ | |
if (SelectedLocation != null) | |
{ | |
Device.BeginInvokeOnMainThread(() => | |
{ | |
AssociatedObject.MoveToRegion(MapSpan.FromCenterAndRadius(SelectedLocation.Value, Distance.FromMeters(500))); | |
}); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using Xamarin.Forms; | |
using Xamarin.Forms.GoogleMaps; | |
namespace YourNamespace.Models | |
{ | |
public class YourModel | |
{ | |
// Note: It can be any object but it has to have these properties at least | |
// Latitude / Longitude value of the pin | |
public Position Position { get; set; } | |
// Title of the callout / bubble of the Pin | |
public string Name { get; set; } | |
// Full Address information for the callout / bubble | |
public string FullAddress { get; set; } | |
// The resource name for the Icon. The converters transforms this value into a Bitmap object for the pin | |
public string Pin { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment