示例#1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            AppContextLiveData.Instance.SetLocale(this);
            Platform.Init(this, savedInstanceState);

            if (AppContextLiveData.Instance.IsPortrait)
            {
                SetContentView(Resource.Layout.EditActivityPortrait);
            }
            else
            {
                SetContentView(Resource.Layout.EditActivityLandscape);
            }

            _id = Intent.GetLongExtra("Id", -1);

            var toolbar = FindViewById <Toolbar>(Resource.Id.toolbar);

            SetActionBar(toolbar);

            ActionBar.SetDisplayShowHomeEnabled(true);
            ActionBar.SetDisplayHomeAsUpEnabled(true);
            ActionBar.SetDisplayShowTitleEnabled(true);
            ActionBar.SetTitle(Resource.String.PoiEditActivity);

            _editTextName      = FindViewById <EditText>(Resource.Id.editTextName);
            _editTextLatitude  = FindViewById <EditText>(Resource.Id.editTextLatitude);
            _editTextLongitude = FindViewById <EditText>(Resource.Id.editTextLongitude);
            _editTextAltitude  = FindViewById <EditText>(Resource.Id.editTextAltitude);
            _buttonFavourite   = FindViewById <ImageView>(Resource.Id.buttonFavourite);
            _buttonOpenWiki    = FindViewById <Button>(Resource.Id.buttonWiki);
            _buttonOpenMap     = FindViewById <Button>(Resource.Id.buttonMap);
            _buttonTeleport    = FindViewById <Button>(Resource.Id.buttonTeleport);
            _spinnerCategory   = FindViewById <Spinner>(Resource.Id.spinnerCategory);
            _spinnerCountry    = FindViewById <Spinner>(Resource.Id.spinnerCountry);

            _spinnerCountry.Adapter = new CountryAdapter(this);

            _spinnerCategory.Adapter = new CategoryAdapter(this);

            _thumbnail = FindViewById <ImageView>(Resource.Id.Thumbnail);

            if (_id != -1)
            {
                _item = Context.Database.GetItem(_id);

                var categoryIndex = (_spinnerCategory.Adapter as CategoryAdapter).GetPosition(_item.Category);
                _spinnerCategory.SetSelection(categoryIndex);

                var countryIndex = (_spinnerCountry.Adapter as CountryAdapter).GetPosition(_item.Country);
                _spinnerCountry.SetSelection(countryIndex);
                _editTextName.Text      = _item.Name;
                _editTextAltitude.Text  = $"{_item.Altitude:F0}";
                _editTextLongitude.Text = $"{_item.Longitude:F7}".Replace(",", ".");
                _editTextLatitude.Text  = $"{_item.Latitude:F7}".Replace(",", ".");

                _thumbnail.SetImageResource(PoiCategoryHelper.GetImage(_item.Category));
            }
            else
            {
                var country      = PoiCountryHelper.GetDefaultCountryByPhoneSettings();
                var countryIndex = (_spinnerCountry.Adapter as CountryAdapter).GetPosition(country);
                _spinnerCountry.SetSelection(countryIndex);
            }

            _buttonFavourite.SetImageResource(_item.Favorite ? Android.Resource.Drawable.ButtonStarBigOn : Android.Resource.Drawable.ButtonStarBigOff);

            _buttonOpenWiki.Enabled    = (string.IsNullOrEmpty(_item.Wikidata) && string.IsNullOrEmpty(_item.Wikipedia)) ? false : true;
            _buttonOpenWiki.Visibility = (string.IsNullOrEmpty(_item.Wikidata) && string.IsNullOrEmpty(_item.Wikipedia)) ? ViewStates.Gone : ViewStates.Visible;

            //finally set-up event listeners
            _editTextName.TextChanged      += OnTextChanged;
            _editTextLatitude.TextChanged  += OnTextChanged;
            _editTextLongitude.TextChanged += OnTextChanged;
            _editTextAltitude.TextChanged  += OnTextChanged;

            _buttonFavourite.SetOnClickListener(this);
            _buttonOpenMap.SetOnClickListener(this);
            _buttonOpenWiki.SetOnClickListener(this);
            _buttonTeleport.SetOnClickListener(this);

            _spinnerCategory.ItemSelected += OnCategorySelected;
            _spinnerCountry.ItemSelected  += OnCountrySelected;
        }
示例#2
0
        public static async Task <PlaceInfo> AsyncGetPlaceName(GpsLocation location)
        {
            //https://docs.microsoft.com/en-us/xamarin/essentials/geocoding?tabs=android

            try
            {
                var loc = new Xamarin.Essentials.Location(location.Latitude, location.Longitude);

                var placemarks = await Geocoding.GetPlacemarksAsync(loc);

                var placemark = placemarks?.FirstOrDefault();
                if (placemark != null)
                {
                    var geocodeAddress = placemark.Thoroughfare;
                    geocodeAddress = geocodeAddress.Append(" ", placemark.SubThoroughfare);
                    geocodeAddress = geocodeAddress.Append(", ", placemark.SubLocality);
                    geocodeAddress = geocodeAddress.Append(", ", placemark.Locality);
                    geocodeAddress = geocodeAddress.Append(", ", placemark.CountryCode);

                    var country = PoiCountryHelper.GetCountry(placemark.CountryCode) ?? PoiCountryHelper.GetDefaultCountryByPhoneSettings();
                    return(new PlaceInfo(geocodeAddress, country));
                }
                return(new PlaceInfo());
            }
            catch
            {
                return(new PlaceInfo());
            }
        }