Exemplo n.º 1
0
        public async Task GetLocation(double lat, double lon)
        {
            City final = null;

            switch (args)
            {
            case LocateRoute.unknown:
                var near = Models.Location.GetNearsetLocation(new Models.Location((float)lat, (float)lon));
                final = near;
                break;

            case LocateRoute.Amap:
                var acontract = await Models.Location.AmapReGeoAsync(lat, lon);

                if (acontract != null)
                {
                    var li = new City
                    {
                        CityEn      = acontract.regeocode.addressComponent.district,
                        CityZh      = acontract.regeocode.addressComponent.district,
                        LeaderEn    = acontract.regeocode.addressComponent.city,
                        LeaderZh    = acontract.regeocode.addressComponent.city,
                        CountryCode = "CN",
                        CountryEn   = acontract.regeocode.addressComponent.country,
                        //Id = await Models.Location.HeWeatherGeoLookup(acontract.regeocode.addressComponent.district),
                        Latitude   = (float)lat,
                        Longitude  = (float)lon,
                        ProvinceEn = acontract.regeocode.addressComponent.province,
                        ProvinceZh = acontract.regeocode.addressComponent.province
                    };
                    final = li;
                }
                break;

            case LocateRoute.Omap:
                var ocontract = await Models.Location.OpenMapReGeoAsync(lat, lon);

                if (ocontract != null && !ocontract.results.IsNullorEmpty())
                {
                    var li = new City
                    {
                        CityEn      = ocontract.results[0].components.county,
                        CityZh      = ocontract.results[0].components.county,
                        LeaderEn    = ocontract.results[0].components.region,
                        LeaderZh    = ocontract.results[0].components.region,
                        CountryCode = ocontract.results[0].components.country_code,
                        CountryEn   = ocontract.results[0].components.country,
                        //Id = await Models.Location.HeWeatherGeoLookup(ocontract.results[0].components.county),
                        Latitude   = (float)lat,
                        Longitude  = (float)lon,
                        ProvinceEn = ocontract.results[0].components.state,
                        ProvinceZh = ocontract.results[0].components.state
                    };
                    final = li;
                }
                break;

            case LocateRoute.IP:
                var id = await Models.Location.ReGeobyIpAsync();

                if (id != null)
                {
                    var f = SQLAction.Find(id.CityId);
                    if (f != null)
                    {
                        final = f;
                    }
                }
                break;

            case LocateRoute.Gmap:
                throw new NotImplementedException("Google map api not implement");
            }

            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, new DispatchedHandler(() =>
            {
                if (final == null)
                {
                    City = new City
                    {
                        CityEn = "定位失败",
                        Latitude = (float)lat,
                        Longitude = (float)lon,
                        CountryCode = "",
                        Id = "",
                    };
                    return;
                }
                City = final;
                Location = new Models.Location(City.Latitude, City.Longitude);
            }));
        }
Exemplo n.º 2
0
        private async void DismissedEventHandler(SplashScreen sender, object args)
        {
            dismissed = true;
            await SQLAction.CheckDB();

            SetLongTimeTimer();
            var settings = SettingsModel.Current;
            var license  = new License.License();

            if (!license.IsPurchased)
            {
                settings.Preferences.EnableAlarm   = false;
                settings.Preferences.EnableEvening = false;
                settings.Preferences.EnableMorning = false;
                settings.Preferences.Set(240);
            }

            if (settings.Preferences.MainColor.A > 0)
            {
                App.MainColor = settings.Preferences.MainColor;
                await Dispatcher.RunAsync(CoreDispatcherPriority.High, new DispatchedHandler(() =>
                {
                    App.ChangeThemeColor(settings.Preferences.MainColor);
                    ChangeThemeColor(settings.Preferences.MainColor);
                }));
            }
            if (!settings.Inited)
            {
                NavigatetoStart();
                return;
            }
            if (!this.args.IsNullorEmpty())
            {
                settings.Cities.ChangeCurrent(this.args);
            }
            try
            {
                if (settings.Cities.GetCurrentCity() == null)
                {
                    NavigatetoStart();
                    return;
                }
            }
            catch (Exception)
            {
            }

            if (settings.Cities.CurrentIndex == -1 && settings.Cities.EnableLocate)
            {
                try
                {
                    var accessStatus = await Geolocator.RequestAccessAsync();

                    if (accessStatus == GeolocationAccessStatus.Allowed)
                    {
                        await Dispatcher.RunAsync(CoreDispatcherPriority.High, new DispatchedHandler(async() =>
                        {
                            var l = new ResourceLoader();
                            SplashWelcome.Text = l.GetString("Locating");
                            try
                            {
                                var _geolocator = new Geolocator();
                                var pos = await _geolocator.GetGeopositionAsync();
                                if ((_geolocator.LocationStatus != PositionStatus.NoData) && (_geolocator.LocationStatus != PositionStatus.NotAvailable) && (_geolocator.LocationStatus != PositionStatus.Disabled))
                                {
                                    var t = ThreadPool.RunAsync(async(w) =>
                                    {
                                        await CalcPosition(pos, settings);
                                        GetCityListandCompelte(settings);
                                    });
                                }
                                else
                                {
                                    GetCityListandCompelte(settings);
                                }
                            }
                            catch (Exception)
                            {
                                GetCityListandCompelte(settings);
                            }
                        }));
                    }
                    else
                    {
                        if (!settings.Cities.SavedCities.IsNullorEmpty())
                        {
                            settings.Cities.CurrentIndex = 0;
                        }
                        else
                        {
                            GetCityListandCompelte(settings);
                        }
                    }
                }
                catch (Exception)
                {
                    NavigatetoStart();
                }
            }
            else if (settings.Cities.CurrentIndex >= 0 && !settings.Cities.SavedCities.IsNullorEmpty())
            {
                foreach (var citty in settings.Cities.SavedCities)
                {
                    if (citty.Latitude == 0 && citty.Longitude == 0)
                    {
                        var tar = SQLAction.Find(citty.Id);
                        citty.Latitude  = tar.Latitude;
                        citty.Longitude = tar.Longitude;
                    }
                }
                settings.Cities.Save();
                GetCityListandCompelte(settings);
            }
            else
            {
                NavigatetoStart();
                return;
            }
        }
Exemplo n.º 3
0
        public async static Task <City> ReverseGeoCode(float lat, float lon, LocateRoute[] routes)
        {
            City final = null;

            foreach (var r in routes)
            {
                switch (r)
                {
                case LocateRoute.unknown:
                    var near = GetNearsetLocation(new Location(lat, lon));
                    final = near;
                    break;

                case LocateRoute.Amap:
                    var acontract = await AmapReGeoAsync(lat, lon);

                    if (acontract != null)
                    {
                        var li = new City
                        {
                            CityEn      = acontract.regeocode.addressComponent.district,
                            CityZh      = acontract.regeocode.addressComponent.district,
                            LeaderEn    = acontract.regeocode.addressComponent.city,
                            LeaderZh    = acontract.regeocode.addressComponent.city,
                            CountryCode = "CN",
                            CountryEn   = acontract.regeocode.addressComponent.country,
                            //Id = await Models.Location.HeWeatherGeoLookup(acontract.regeocode.addressComponent.district),
                            Latitude   = (float)lat,
                            Longitude  = (float)lon,
                            ProvinceEn = acontract.regeocode.addressComponent.province,
                            ProvinceZh = acontract.regeocode.addressComponent.province
                        };
                        final = li;
                    }
                    break;

                case LocateRoute.Omap:
                    var ocontract = await OpenMapReGeoAsync(lat, lon);

                    if (ocontract != null && !ocontract.results.IsNullorEmpty())
                    {
                        var li = new City
                        {
                            CityEn      = ocontract.results[0].components.county,
                            CityZh      = ocontract.results[0].components.county,
                            LeaderEn    = ocontract.results[0].components.region,
                            LeaderZh    = ocontract.results[0].components.region,
                            CountryCode = ocontract.results[0].components.country_code,
                            CountryEn   = ocontract.results[0].components.country,
                            //Id = await HeWeatherGeoLookup(ocontract.results[0].components.county),
                            Latitude   = lat,
                            Longitude  = lon,
                            ProvinceEn = ocontract.results[0].components.state,
                            ProvinceZh = ocontract.results[0].components.state
                        };
                        final = li;
                    }
                    break;

                case LocateRoute.IP:
                    var id = await ReGeobyIpAsync();

                    if (id != null)
                    {
                        final = SQLAction.Find(id.CityId);
                    }
                    break;

                case LocateRoute.Gmap:

                    break;

                default:
                    break;
                }
                if (final != null)
                {
                    break;
                }
            }

            if (final != null)
            {
                return(final);
            }
            else
            {
                throw new Exception("Locate Failed.");
            }
        }