private async void ListHostMap_Refreshing(object sender, EventArgs e) { listHostMap.IsRefreshing = true; if (!await LocationHelper.IsGeolocationAvailable()) { listHostMap.IsRefreshing = false; return; } var location = await LocationHelper.GetCurrentPosition(50); if (location == null) { listHostMap.IsRefreshing = false; return; } listHost2 = listHostOriginal.ToList(); foreach (var host in listHost2) { host.distance = location.CalculateDistance(new Plugin.Geolocator.Abstractions.Position(host.partner_latitude, host.partner_longitude), GeolocatorUtils.DistanceUnits.Kilometers); host.distance_display = host.distance > 1 ? host.distance.ToString("N0") + " km" : (host.distance / 1000).ToString("N0") + " m"; } listHostMap.ItemsSource = null; listHostMap.ItemsSource = listHost2.OrderBy(h => h.distance).ToList(); listHostMap.IsRefreshing = false; }
/* * private void pinMapsPlaces() * { * * foreach (var place in listPlaces) * { * ImageSource image = !(place.image is string base64) ? null : ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(base64))); * var img = Convert.FromBase64String(place.image); * var icons = Android.Graphics.BitmapFactory.DecodeByteArray(img, 0, img.Length); * var bitmap = Android.Graphics.Bitmap.CreateBitmap(icons); * * var contents = new StreamContent(new MemoryStream(img)); * * * var abc = Base64.EncodeToString(img, Base64Flags.Default); * maps.Pins.Add(new Pin() * { * Label = place.name, * //Address = host.province, * Icon = BitmapDescriptorFactory.FromView( * new ContentView * { * WidthRequest = 10, * HeightRequest = 10, * Content = new Image * { * Source = image * } * * }), * Position = new Xamarin.Forms.GoogleMaps.Position(place.latitude, place.longitude), * Type = PinType.Place, * BindingContext = place, * ZIndex = place.id, * IsDraggable = false * }); * } * } */ private async Task GetNearestPlaces() { if (!await LocationHelper.IsGeolocationAvailable()) { return; } var location = await LocationHelper.GetCurrentPosition(50); maps.MoveToRegion(MapSpan.FromCenterAndRadius(new Xamarin.Forms.GoogleMaps.Position(location.Latitude, location.Longitude), Distance.FromMeters(1000))); //listVisit = listVisit.ToList(); foreach (var place in listPlaces) { place.distance = location.CalculateDistance(new Plugin.Geolocator.Abstractions.Position(place.latitude, place.longitude), GeolocatorUtils.DistanceUnits.Kilometers); place.distance_display = place.distance > 1 ? place.distance.ToString("N0") + " km" : (place.distance / 1000).ToString("N0") + " m"; } }
private async Task GetNearestHosts() { if (!await LocationHelper.IsGeolocationAvailable()) { return; } var location = await LocationHelper.GetCurrentPosition(50); if (location == null) { //listHostMap.IsRefreshing = false; return; } //maps.MoveToRegion(MapSpan.FromCenterAndRadius(new Xamarin.Forms.GoogleMaps.Position(location.Latitude, location.Longitude), Distance.FromMeters(1000))); listHost1 = listHostOriginal.ToList(); foreach (var host in listHost1) { host.distance = location.CalculateDistance(new Plugin.Geolocator.Abstractions.Position(host.partner_latitude, host.partner_longitude), GeolocatorUtils.DistanceUnits.Kilometers); host.distance_display = host.distance > 1 ? host.distance.ToString("N0") + " km" : (host.distance / 1000).ToString("N0") + " m"; } //listHostInfo.ItemsSource = null; //listHostInfo.ItemsSource = listHost1.OrderBy(h => h.distance).ToList(); //Items = listHost1.OrderBy(h => h.distance).ToList(); listHost2 = listHostOriginal.ToList(); foreach (var host in listHost2) { host.distance = location.CalculateDistance(new Plugin.Geolocator.Abstractions.Position(host.partner_latitude, host.partner_longitude), GeolocatorUtils.DistanceUnits.Kilometers); host.distance_display = host.distance > 1 ? host.distance.ToString("N0") + " km" : (host.distance / 1000).ToString("N0") + " m"; } //listHostMap.ItemsSource = null; //listHostMap.ItemsSource = listHost2.OrderBy(h => h.distance).ToList(); }
private async void FetchPlaces() { UserDialogs.Instance.Loading(title: "Processing Get Information...").Show(); var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(20) }; var auth = "H6V$36A*!?L^G2NXX7U%=GY@"; JObject jsonDataObject = new JObject { { "auth", auth } }; JObject jsonData = new JObject { { "params", jsonDataObject } }; var data = jsonData.ToString(); var content = new StringContent(data, Encoding.UTF8, "application/json"); Debug.WriteLine("REQUEST-FetchPlaces: " + data); try { HttpResponseMessage response = await client.PostAsync(ApiUri.BASE_URL + ApiUri.FETCH_PLACES, content); string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK) { UserDialogs.Instance.Loading().Hide(); Debug.WriteLine("RESPONSE-FetchPlaces: " + responseContent); ListPlacesResponse listPlacesResponse = JsonConvert.DeserializeObject <ListPlacesResponse>(responseContent, App.DefaultSerializerSettings); ListPlacesResult listPlacesResult = listPlacesResponse.Result; listPlaces = listPlacesResult.places.ToList(); var location = await LocationHelper.GetCurrentPosition(50); maps.MoveToRegion(MapSpan.FromCenterAndRadius(new Xamarin.Forms.GoogleMaps.Position(location.Latitude, location.Longitude), Distance.FromMeters(1000))); foreach (var place in listPlaces) { place.distance = location.CalculateDistance(new Plugin.Geolocator.Abstractions.Position(place.latitude, place.longitude), GeolocatorUtils.DistanceUnits.Kilometers); place.distance_display = place.distance > 1 ? place.distance.ToString("N0") + " km" : (place.distance / 1000).ToString("N0") + " m"; } listPlaceOfInterest.ItemsSource = listPlaces .Where(x => x.longitude != 0 && x.latitude != 0) .OrderByDescending(x => x.id) .ToList(); await FetchPlaceTypes(); await GetNearestPlaces(); } } catch (TaskCanceledException ex) { UserDialogs.Instance.Loading().Hide(); UserDialogs.Instance.Toast(new ToastConfig("Bad Connection Error. Try Again")); Debug.WriteLine(ex.Message); } catch (Exception exx) { Debug.WriteLine(exx.Message); UserDialogs.Instance.Loading().Hide(); Notifications.Internal.ServerError(); } }
private async void SubmitPlace() { try { UserDialogs.Instance.Loading(title: "Add place...").Show(); var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(30) }; var AuthService = DependencyService.Get <IAuthService>(); JObject jsonAuth = new JObject { { "login", AuthService.UserName }, { "password", AuthService.Password }, { "db", ServerAuth.DB } }; string imageBase64 = ""; if (imagefile != null) { byte[] imageBytes = await StorageHelper.LoadImage(imagefile.Path); imageBase64 = Convert.ToBase64String(imageBytes); } JObject jsonReview = new JObject { { "title", txtName.Text?.Trim() ?? "" }, { "text", txtContent.Text?.Trim() ?? "" }, { "image", imageBase64 }, { "rating", rating.Rating } }; var location = await LocationHelper.GetCurrentPosition(50); JObject jsonPlace = new JObject { { "name", txtName.Text?.Trim() ?? "" }, { "place_id", typePlace == null? "" : typePlace.id + "" }, { "latitude", location == null? 0 : location.Latitude }, { "longitude", location == null? 0 : location.Longitude } }; JObject jsonDataObject = new JObject { { "auth", jsonAuth }, { "review", jsonReview }, { "place", jsonPlace } }; JObject jsonData = new JObject { { "params", jsonDataObject } }; var data = jsonData.ToString(); var content = new StringContent(data, Encoding.UTF8, "application/json"); var response = await client.PostAsync(ApiUri.BASE_URL + ApiUri.CREATE_PLACE, content); string responseContent = await response.Content.ReadAsStringAsync(); if (response.StatusCode == HttpStatusCode.OK) { SuccessResponse successResponse = JsonConvert.DeserializeObject <SuccessResponse>(responseContent, App.DefaultSerializerSettings); if (successResponse != null && successResponse.Result != null) { if (successResponse.Result.Success) { DependencyService.Get <IToast>().LongAlert("Add new place success!"); await Navigation.PopAsync(true); if (this._updateResultListener != null) { this._updateResultListener.OnUpdate(true); } } else if (!string.IsNullOrWhiteSpace(successResponse.Result.Message)) { UserDialogs.Instance.Toast(new ToastConfig(successResponse.Result.Message)); } } else { Internal.ServerError(); } } else { Internal.ServerError(); } } catch (TaskCanceledException e) { UserDialogs.Instance.Toast(new ToastConfig("Bad Connection Error. Try Again")); } catch (Exception e) { Internal.ServerError(); } finally { UserDialogs.Instance.Loading().Hide(); } }