Created
April 8, 2015 15:39
-
-
Save rafaelrmou/4e0fb16d530f2d16c368 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 AmigoDaCarona_P.Models; | |
using AmigoDaCarona_P.Utils; | |
using Geolocator.Plugin; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Xam.Plugin.MapExtend.Abstractions; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Maps; | |
namespace AmigoDaCarona_P.UI | |
{ | |
public class EnderecoEdit : CustomContentPage<EnderecoEdit> | |
{ | |
public EnderecoEdit() | |
{ | |
CriarTela(); | |
} | |
private void CriarTela() | |
{ | |
var map = new MapExtend(MapSpan.FromCenterAndRadius(new Position(31, -122), Xamarin.Forms.Maps.Distance.FromMiles(0.3))) | |
{ | |
IsShowingUser = true, | |
HeightRequest = 100, | |
WidthRequest = 960, | |
VerticalOptions = LayoutOptions.FillAndExpand | |
}; | |
var entryEnd = new Entry | |
{ | |
Placeholder = "Endereço", | |
HorizontalOptions = LayoutOptions.FillAndExpand | |
}; | |
var btnSearch = new Button | |
{ | |
Text = "P", | |
BackgroundColor = Color.Transparent, | |
BorderColor = Color.Transparent | |
}; | |
var btnCreateRota = new Button | |
{ | |
Text = "Direções", | |
BackgroundColor = Color.Transparent, | |
BorderColor = Color.Transparent | |
}; | |
btnSearch.Clicked += async (sender, args) => | |
{ | |
await map.SearchAdress(entryEnd.Text).ContinueWith(t => | |
{ | |
if (t.Exception != null) | |
DisplayAlert("", t.Exception.Message, "ok"); | |
}); | |
}; | |
var barItens = new StackLayout | |
{ | |
Orientation = StackOrientation.Horizontal | |
}; | |
barItens.Children.Add(entryEnd); | |
barItens.Children.Add(btnSearch); | |
var stack = new StackLayout { Spacing = 0 }; | |
stack.Children.Add(barItens); | |
stack.Children.Add(map); | |
var btnSalvar = new Button() { Text = "Salvar", HorizontalOptions = LayoutOptions.FillAndExpand }; | |
btnSalvar.Clicked += async (sender, args) => | |
{ | |
var Ruas = (await (new Geocoder().GetAddressesForPositionAsync(map.Pins[0].Position))).ToList(); | |
if (!Ruas.Any()) | |
{ | |
await DisplayAlert("Pos", "Falha ao buscar o endereço", "OK"); | |
return; | |
} | |
string texto = string.Format("{0}, {1}, | |
{2}", map.Pins[0].Position.Latitude.ToString(), | |
map.Pins[0].Position.Longitude.ToString(), | |
Ruas[0]); | |
await DisplayAlert("Pos", texto, "OK"); | |
}; | |
stack.Children.Add(btnSalvar); | |
btnCreateRota.Clicked += async (sender, args) => | |
{ | |
await map.CreateRoute(map.Pins[0].Position, map.Pins[1].Position).ContinueWith(t => | |
{ | |
if (t.Exception != null) | |
DisplayAlert("", t.Exception.Message, "ok"); | |
}); | |
}; | |
stack.Children.Add(btnCreateRota); | |
var btnNearbyLocation = new Button() | |
{ | |
Text = "Locais proximos" | |
}; | |
btnNearbyLocation.Clicked += async (sender, args) => | |
{ | |
await map.NearbyLocations("AIzaSyBuATAkE41ioaMXd6MvWOmFlG2p-MlE6HM","").ContinueWith(t => | |
{ | |
if (t.Exception != null) | |
DisplayAlert("", t.Exception.Message, "ok"); | |
}); | |
}; | |
stack.Children.Add(btnNearbyLocation); | |
Content = stack; | |
var locator = CrossGeolocator.Current; | |
locator.DesiredAccuracy = 50; | |
var geoLocation = locator.GetPositionAsync().ContinueWith(t => | |
{ | |
if (t.IsFaulted) | |
{ | |
//System.Diagnostics.Debug.WriteLine("Error : {0}", ((GeolocationException)t.Exception.InnerException).Error.ToString()); | |
} | |
else if (t.IsCanceled) | |
{ | |
System.Diagnostics.Debug.WriteLine("Error : The geolocation has got canceled !"); | |
} | |
else | |
{ | |
var currentLocation = new Xamarin.Forms.Maps.Position(t.Result.Latitude, t.Result.Longitude); | |
Device.BeginInvokeOnMainThread(() => | |
{ | |
map.MoveToRegion(MapSpan.FromCenterAndRadius(currentLocation, Xamarin.Forms.Maps.Distance.FromMiles(0.5))); | |
map.Pins.Add(new Pin { Label = "seu endereço", Position = currentLocation }); | |
}); | |
} | |
}, TaskScheduler.FromCurrentSynchronizationContext()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment