Created
June 5, 2019 19:40
-
-
Save rdelrosario/0776a0b01ea9663abff762c6c118d23d to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Windows.Input; | |
using TrackingSample.Views; | |
using Xamarin.Essentials; | |
using Xamarin.Forms; | |
using Xamarin.Forms.GoogleMaps; | |
namespace TrackingSample | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
public static readonly BindableProperty CalculateCommandProperty = | |
BindableProperty.Create(nameof(CalculateCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay); | |
public ICommand CalculateCommand | |
{ | |
get { return (ICommand)GetValue(CalculateCommandProperty); } | |
set { SetValue(CalculateCommandProperty, value); } | |
} | |
public static readonly BindableProperty UpdateCommandProperty = | |
BindableProperty.Create(nameof(UpdateCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay); | |
public ICommand UpdateCommand | |
{ | |
get { return (ICommand)GetValue(UpdateCommandProperty); } | |
set { SetValue(UpdateCommandProperty, value); } | |
} | |
public MainPage() | |
{ | |
InitializeComponent(); | |
CalculateCommand = new Command<List<Xamarin.Forms.GoogleMaps.Position>>(Calculate); | |
UpdateCommand = new Command<Xamarin.Forms.GoogleMaps.Position>(Update); | |
GetActualLocationCommand = new Command(async () => await GetActualLocation()); | |
} | |
async void Update(Xamarin.Forms.GoogleMaps.Position position) | |
{ | |
if (map.Pins.Count == 1 && map.Polylines!=null&& map.Polylines?.Count>1) | |
return; | |
var cPin = map.Pins.FirstOrDefault(); | |
if (cPin != null) | |
{ | |
cPin.Position = new Position(position.Latitude, position.Longitude); | |
cPin.Icon = BitmapDescriptorFactory.FromView(new Image() { Source = "ic_taxi.png", WidthRequest = 25, HeightRequest = 25 }); | |
await map.MoveCamera(CameraUpdateFactory.NewPosition(new Position(position.Latitude, position.Longitude))); | |
var previousPosition = map?.Polylines?.FirstOrDefault()?.Positions?.FirstOrDefault(); | |
map.Polylines?.FirstOrDefault()?.Positions?.Remove(previousPosition.Value); | |
} | |
else | |
{ | |
//END TRIP | |
map.Polylines?.FirstOrDefault()?.Positions?.Clear(); | |
} | |
} | |
void Calculate(List<Xamarin.Forms.GoogleMaps.Position> list) | |
{ | |
searchLayout.IsVisible = false; | |
stopRouteButton.IsVisible = true; | |
map.Polylines.Clear(); | |
var polyline = new Xamarin.Forms.GoogleMaps.Polyline(); | |
foreach (var p in list) | |
{ | |
polyline.Positions.Add(p); | |
} | |
map.Polylines.Add(polyline); | |
map.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(polyline.Positions[0].Latitude, polyline.Positions[0].Longitude), Xamarin.Forms.GoogleMaps.Distance.FromMiles(0.50f))); | |
var pin = new Xamarin.Forms.GoogleMaps.Pin | |
{ | |
Type = PinType.Place, | |
Position = new Position(polyline.Positions.First().Latitude, polyline.Positions.First().Longitude), | |
Label = "First", | |
Address = "First", | |
Tag = string.Empty, | |
Icon = BitmapDescriptorFactory.FromView(new Image() { Source = "ic_taxi.png", WidthRequest = 25, HeightRequest = 25 }) | |
}; | |
map.Pins.Add(pin); | |
var pin1 = new Xamarin.Forms.GoogleMaps.Pin | |
{ | |
Type = PinType.Place, | |
Position = new Position(polyline.Positions.Last().Latitude, polyline.Positions.Last().Longitude), | |
Label = "Last", | |
Address = "Last", | |
Tag = string.Empty | |
}; | |
map.Pins.Add(pin1); | |
} | |
public async void OnEnterAddressTapped(object sender, EventArgs e) | |
{ | |
await Navigation.PushAsync(new SearchPlacePage() { BindingContext = this.BindingContext }, false); | |
} | |
public void Handle_Stop_Clicked(object sender, EventArgs e) | |
{ | |
searchLayout.IsVisible = true; | |
stopRouteButton.IsVisible = false; | |
map.Polylines.Clear(); | |
map.Pins.Clear(); | |
} | |
//Center map in actual location | |
protected override void OnAppearing() | |
{ | |
base.OnAppearing(); | |
GetActualLocationCommand.Execute(null); | |
} | |
public static readonly BindableProperty GetActualLocationCommandProperty = | |
BindableProperty.Create(nameof(GetActualLocationCommand), typeof(ICommand), typeof(MainPage), null, BindingMode.TwoWay); | |
public ICommand GetActualLocationCommand | |
{ | |
get { return (ICommand)GetValue(GetActualLocationCommandProperty); } | |
set { SetValue(GetActualLocationCommandProperty, value); } | |
} | |
async Task GetActualLocation() | |
{ | |
try | |
{ | |
var request = new GeolocationRequest(GeolocationAccuracy.High); | |
var location = await Geolocation.GetLocationAsync(request); | |
if (location != null) | |
{ | |
map.MoveToRegion(MapSpan.FromCenterAndRadius( | |
new Position(location.Latitude, location.Longitude), Distance.FromMiles(0.3))); | |
} | |
} | |
catch (Exception ex) | |
{ | |
await DisplayAlert("Error", "Unable to get actual location", "Ok"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment