示例#1
0
        /// <summary>
        /// Search dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void SearchMap(string addr, string name)
        {
            try
            {
                var geocoder  = new Xamarin.Forms.GoogleMaps.Geocoder();
                var positions = await geocoder.GetPositionsForAddressAsync(addr);

                if (positions != null && positions.Count() > 0)
                {
                    if (map.Pins.Count > 0)
                    {
                        map.Pins.Clear();
                    }
                    var pos = positions.First();
                    map.MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMeters(1000)));
                    Pin pinLocation = new Pin()
                    {
                        Type     = PinType.Place,
                        Label    = name,
                        Address  = addr,
                        Position = pos
                    };
                    Position.Text = $"Tọa độ - {PrintPin(pinLocation)}";
                    if (model != null)
                    {
                        model.Position = pos;
                    }
                    pinLocation.IsDraggable = true;
                    map.Pins.Add(pinLocation);
                    map.SelectedPin = pinLocation;
                }
                else
                {
                    await this.DisplayAlert("Not found", "Geocoder returns no results", "Close");
                }
            }catch (Exception e)
            {
                var answer = await DisplayAlert("Error", "Ấn \"Yes\" để reload lại map hoặc \"No\" để quay trở lại", "Yes", "No");

                if (answer)
                {
                    SearchMap(model.Addr, model.Name);
                }
                else
                {
                    UserDialogs.Instance.Toast("Không có dữ liệu tọa độ của club");
                    if (model != null)
                    {
                        model.BackPage();
                    }
                }
            }
        }
示例#2
0
        public async void GeoCode(String address)
        {
            var geocoder  = new Xamarin.Forms.GoogleMaps.Geocoder();
            var positions = await geocoder.GetPositionsForAddressAsync(address);

            if (positions.Count() > 0)
            {
                var pos = positions.First();
                map.MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMeters(5000)));
                var reg = map.VisibleRegion;
                //var format = "0.00";
                //labelStatus.Text = $"Center = {reg.Center.Latitude.ToString(format)}, {reg.Center.Longitude.ToString(format)}";
            }
            else
            {
                //await this.DisplayAlert("Not found", "No results", "Close");
                //Debug.WriteLine("Geocoder returns no results");
            }
        }
        public BasicMapPage()
        {
            InitializeComponent();

            // MapTypes
            var mapTypeValues = new List <MapType>();

            foreach (var mapType in Enum.GetValues(typeof(MapType)))
            {
                mapTypeValues.Add((MapType)mapType);
                pickerMapType.Items.Add(Enum.GetName(typeof(MapType), mapType));
            }

            pickerMapType.SelectedIndexChanged += (sender, e) =>
            {
                map.MapType = mapTypeValues[pickerMapType.SelectedIndex];
            };
            pickerMapType.SelectedIndex = 0;


            // ZoomEnabled
            switchHasZoomEnabled.Toggled += (sender, e) =>
            {
                map.HasZoomEnabled = e.Value;
            };
            switchHasZoomEnabled.IsToggled = map.HasZoomEnabled;

            // ScrollEnabled
            switchHasScrollEnabled.Toggled += (sender, e) =>
            {
                map.HasScrollEnabled = e.Value;
            };
            switchHasScrollEnabled.IsToggled = map.HasScrollEnabled;

            // IsShowingUser
            switchIsShowingUser.Toggled += (sender, e) =>
            {
                map.IsShowingUser = e.Value;
            };
            switchIsShowingUser.IsToggled = map.IsShowingUser;

            // Map Clicked
            map.MapClicked += (sender, e) =>
            {
                var lat = e.Point.Latitude.ToString("0.000");
                var lng = e.Point.Longitude.ToString("0.000");
                this.DisplayAlert("MapClicked", $"{lat}/{lng}", "CLOSE");
            };

            // Map Long clicked
            map.MapLongClicked += (sender, e) =>
            {
                var lat = e.Point.Latitude.ToString("0.000");
                var lng = e.Point.Longitude.ToString("0.000");
                this.DisplayAlert("MapLongClicked", $"{lat}/{lng}", "CLOSE");
            };

            // Geocode
            buttonGeocode.Clicked += async(sender, e) =>
            {
                var geocoder  = new Xamarin.Forms.GoogleMaps.Geocoder();
                var positions = await geocoder.GetPositionsForAddressAsync(entryAddress.Text);

                if (positions.Count() > 0)
                {
                    var pos = positions.First();
                    map.MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMeters(5000)));
                    var reg    = map.VisibleRegion;
                    var format = "0.00";
                    labelStatus.Text = $"Center = {reg.Center.Latitude.ToString(format)}, {reg.Center.Longitude.ToString(format)}";
                }
                else
                {
                    await this.DisplayAlert("Not found", "Geocoder returns no results", "Close");
                }
            };
        }
        public BasicMapPage()
        {
            InitializeComponent();

            // MapTypes
            var mapTypeValues = new List <MapType>();

            foreach (var mapType in Enum.GetValues(typeof(MapType)))
            {
                mapTypeValues.Add((MapType)mapType);
                pickerMapType.Items.Add(Enum.GetName(typeof(MapType), mapType));
            }

            pickerMapType.SelectedIndexChanged += (sender, e) =>
            {
                map.MapType = mapTypeValues[pickerMapType.SelectedIndex];
            };
            pickerMapType.SelectedIndex = 0;


            // ZoomEnabled
            switchHasZoomEnabled.Toggled += (sender, e) =>
            {
                map.HasZoomEnabled = e.Value;
            };
            switchHasZoomEnabled.IsToggled = map.HasZoomEnabled;

            // ScrollEnabled
            switchHasScrollEnabled.Toggled += (sender, e) =>
            {
                map.HasScrollEnabled = e.Value;
            };
            switchHasScrollEnabled.IsToggled = map.HasScrollEnabled;

            // IsShowingUser
            switchIsShowingUser.Toggled += (sender, e) =>
            {
                map.IsShowingUser = e.Value;
            };
            switchIsShowingUser.IsToggled = map.IsShowingUser;

            // IsTrafficEnabled
            switchIsTrafficEnabled.Toggled += (sender, e) =>
            {
                map.IsTrafficEnabled = e.Value;
            };
            switchIsTrafficEnabled.IsToggled = map.IsTrafficEnabled;

            SwitchIndoorEnabled.Toggled += (sender, e) =>
            {
                map.IsIndoorEnabled = e.Value;
            };
            SwitchIndoorEnabled.IsToggled = map.IsIndoorEnabled;

            // Map Clicked
            map.MapClicked += (sender, e) =>
            {
                var lat = e.Point.Latitude.ToString("0.000");
                var lng = e.Point.Longitude.ToString("0.000");
                this.DisplayAlert("MapClicked", $"{lat}/{lng}", "CLOSE");
            };

            // Map Long clicked
            map.MapLongClicked += (sender, e) =>
            {
                var lat = e.Point.Latitude.ToString("0.000");
                var lng = e.Point.Longitude.ToString("0.000");
                this.DisplayAlert("MapLongClicked", $"{lat}/{lng}", "CLOSE");
            };

            // Map MyLocationButton clicked
            map.MyLocationButtonClicked += (sender, args) =>
            {
                args.Handled = switchHandleMyLocationButton.IsToggled;
                if (switchHandleMyLocationButton.IsToggled)
                {
                    this.DisplayAlert("MyLocationButtonClicked",
                                      "If set MyLocationButtonClickedEventArgs.Handled = true then skip obtain current location",
                                      "OK");
                }
            };

            map.CameraChanged += (sender, args) =>
            {
                var p = args.Position;
                labelStatus.Text = $"Lat={p.Target.Latitude:0.00}, Long={p.Target.Longitude:0.00}, Zoom={p.Zoom:0.00}, Bearing={p.Bearing:0.00}, Tilt={p.Tilt:0.00}";
            };

            // Geocode
            buttonGeocode.Clicked += async(sender, e) =>
            {
                var geocoder  = new Xamarin.Forms.GoogleMaps.Geocoder();
                var positions = await geocoder.GetPositionsForAddressAsync(entryAddress.Text);

                if (positions.Count() > 0)
                {
                    var pos = positions.First();
                    map.MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMeters(5000)));
                    var reg    = map.VisibleRegion;
                    var format = "0.00";
                    labelStatus.Text = $"Center = {reg.Center.Latitude.ToString(format)}, {reg.Center.Longitude.ToString(format)}";
                }
                else
                {
                    await this.DisplayAlert("Not found", "Geocoder returns no results", "Close");
                }
            };

            // Snapshot
            buttonTakeSnapshot.Clicked += async(sender, e) =>
            {
                var stream = await map.TakeSnapshot();

                imageSnapshot.Source = ImageSource.FromStream(() => stream);
            };
        }