private void Locator_PositionChanged(object sender, PositionEventArgs e)
        {
            var position = e.Position;

            dialogs.Toast($"{position.Latitude} | {position.Longitude}", TimeSpan.FromSeconds(1));

            var location = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);
            var mapSpan  = MapSpan.FromCenterAndRadius(location, Distance.FromMeters(100));

            var lastPos = Model.Route.LastOrDefault();

            if (lastPos == null || (lastPos != null && Distance.BetweenPositions(location, lastPos).Meters > 5))
            {
                Model.AddPosition(location);
                viewMap.MapElements.Clear();
                var route = new Polyline()
                {
                    StrokeColor = Color.FromHex("#fc6203"),
                    StrokeWidth = 10
                };

                foreach (var pos in Model.Route)
                {
                    route.Geopath.Add(pos);
                }
                viewMap.MapElements.Add(route);
            }

            viewMap.MoveToRegion(mapSpan);
        }
Example #2
0
        public void ConstructFromPositions()
        {
            const double EPSILON = 0.001;

            Position position1 = new Position(37.403992, -122.034988);
            Position position2 = new Position(37.776691, -122.416534);

            Distance distance = Distance.BetweenPositions(position1, position2);

            Assert.True(Math.Abs(distance.Meters - 53363.08) < EPSILON);
            Assert.True(Math.Abs(distance.Kilometers - 53.36308) < EPSILON);
            Assert.True(Math.Abs(distance.Miles - 33.15828) < EPSILON);
        }
Example #3
0
 private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
 {
     if (SelectedTempat != null && e.Position.Accuracy > 50)
     {
         var m = map.Pins.Where(x => x.AutomationId == SelectedTempat.TempatId.ToString()).FirstOrDefault();
         if (m != null)
         {
             Distance distance4 = Distance.BetweenPositions(new Position(e.Position.Latitude, e.Position.Longitude), m.Position);
             SelectedTempat.Distance = distance4;
         }
         AbsenCommand.ChangeCanExecute();
     }
     else
     {
         Status = "Anda Belum Memilik Tempat";
     }
 }
Example #4
0
        private async void Load()
        {
            tempats = await DataService.GetTempatAbsen();

            var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));

            cts = new CancellationTokenSource();
            var location = await Geolocation.GetLocationAsync(request, cts.Token);

            if (location != null)
            {
                var newMap = new MapSpan(new Position(location.Latitude, location.Longitude), 0.01, 0.01);

                foreach (var item in tempats)
                {
                    var      position  = new Position(item.Tempat.Latitude, item.Tempat.Longitude);
                    Distance distance4 = Distance.BetweenPositions(new Position(location.Latitude, location.Longitude), position);
                    item.Distance = distance4;
                    Pin pin = new Pin
                    {
                        Label        = $"Distance {distance4.Meters} M",
                        Address      = $"{item.Tempat.Nama}, {item.Tempat.Address}",
                        Type         = PinType.Place,
                        AutomationId = item.TempatId.ToString(),
                        Position     = position
                    };

                    pin.Clicked += Pin_Clicked;
                    map.Pins.Add(pin);
                }

                if (tempats != null && tempats.Count() >= 0)
                {
                    var item = tempats.OrderBy(x => x.Distance.Meters).FirstOrDefault();
                    newMap = new MapSpan(new Position(item.Tempat.Latitude, item.Tempat.Longitude), 0.01, 0.01);
                }

                map.MoveToRegion(newMap);

                if (!CrossGeolocator.Current.IsListening)
                {
                    await CurrentPositon.StartListening();
                }
            }
        }
Example #5
0
        async void ExecuteLoadPinsCommand()
        {
            List <string> adressList = currentProducts.Select(x => x.adress).Distinct().ToList();

            map.Pins.Clear();
            foreach (var adress in adressList)
            {
                map.Pins.Add(new Pin {
                    Label    = currentProducts.Where(x => x.adress == adress).Select(x => x.sellerName).First(),
                    Address  = adress,
                    Type     = PinType.Place,
                    Position = await MapService.getCoordinates(adress)
                });
            }

            foreach (Pin pin in map.Pins)
            {
                pin.InfoWindowClicked += async(obj, args) =>
                {
                    sB.Text = ((Pin)obj).Label;
                    await App.Current.MainPage.Navigation.PopToRootAsync();
                };
            }

            double south = map.Pins.Min(pin => pin.Position.Latitude);
            double north = map.Pins.Max(pin => pin.Position.Latitude);
            double west  = map.Pins.Min(pin => pin.Position.Longitude);
            double east  = map.Pins.Max(pin => pin.Position.Longitude);

            Position center = new Position((south + north) / 2, (west + east) / 2);

            Distance latidudinal  = Distance.BetweenPositions(new Position(north, center.Longitude), new Position(south, center.Longitude));
            Distance longitudinal = Distance.BetweenPositions(new Position(center.Latitude, west), new Position(center.Latitude, east));


            Distance radius = (latidudinal.Kilometers > longitudinal.Kilometers) ? latidudinal : longitudinal;

            map.MoveToRegion(MapSpan.FromCenterAndRadius(center, radius));
        }
Example #6
0
        void MapClicked(object sender, MapClickedEventArgs e)
        {
            switch (_selectedType)
            {
            case SelectedElementType.Polyline:
                _polyline.Geopath.Add(e.Position);
                break;

            case SelectedElementType.Polygon:
                _polygon.Geopath.Add(e.Position);
                break;

            case SelectedElementType.Circle:
                if (_circle.Center == default(Position))
                {
                    _circle.Center = e.Position;
                }
                else
                {
                    _circle.Radius = Distance.BetweenPositions(_circle.Center, e.Position);
                }
                break;
            }
        }
        async Task ExecuteLoadItemsCommand()
        {
            IsBusy = true;
            Location       location;
            List <Product> products = new List <Product>();

            try
            {
                products = (List <Product>) await ProductService.GetProductsAsync(true);

                List <string> adressList = products.Select(x => x.adress).Distinct().ToList();

                var adr = new Dictionary <string, Position>();
                foreach (var adress in adressList)
                {
                    adr.Add(adress, await MapService.getCoordinates(adress));
                }
                adressCoordinates = adr;


                var request = new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10));
                location = await Geolocation.GetLocationAsync(request, new CancellationTokenSource().Token);
            }
            catch (PermissionException e)
            {
                location = null;
                Debug.WriteLine(e);
            }
            catch (Exception ex)
            {
                location = null;
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }

            Products.Clear();


            for (int i = 0; i < products.Count(); i++)
            {
                var product = products[i];

                var pos = adressCoordinates[product.adress];
                if (location == null)
                {
                    product.distance = 0;
                }
                else
                {
                    product.distance = Math.Round(Distance.BetweenPositions(pos, new Position(location.Latitude, location.Longitude)).Kilometers, 2);
                }
                product.imagePath = $"http://{Secrets.IP}/images/{product.name.ToLower()}";

                if (i % 2 == 0)
                {
                    product.backgroundColor = Color.FromHex("#FFFFFF");
                }
                else
                {
                    product.backgroundColor = Color.FromHex("#F0F0F0");
                }

                Products.Add(product);
            }
        }