通过高德定位取温度信息

1,022 阅读1分钟

通过高德定位取温度信息

   /**
     * 通过定位实时获取天气数据
     */
    public Observable<LocalWeatherLive> getRunTimeWeatherLive(AMapLocation aMapLocation) {
        if (localWeatherLive != null && (System.currentTimeMillis() - lastUpdateLocationTimeForWeather) < GO_LOCATION_WEATHER_TIME) {
            return Observable.just(localWeatherLive);
        }
        return getRegeocodeSearched(MBUtil.getContext(), new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude()), 200)
                .flatMap(addressModel -> getWeatherLive(MBUtil.getContext(), addressModel.getCity()))
                .doOnNext(localWeatherLive -> {
                    MBLocationManager.this.localWeatherLive = localWeatherLive;
                    lastUpdateLocationTimeForWeather = System.currentTimeMillis();
                })
                .observeOn(AndroidSchedulers.mainThread());
    }

/**
     * 获取实况天气
     */
    public Observable<LocalWeatherLive> getWeatherLive(Context context, String city) {
        return Observable.create(new ObservableOnSubscribe<LocalWeatherLive>() {
            @Override
            public void subscribe(ObservableEmitter<LocalWeatherLive> e) throws Exception {
                WeatherSearch weatherLiveSearch = new WeatherSearch(context);
                weatherLiveSearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
                    @Override
                    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int rCode) {
                        if (rCode == 1000 && localWeatherLiveResult != null && localWeatherLiveResult.getLiveResult() != null) {
                            e.onNext(localWeatherLiveResult.getLiveResult());
                            e.onComplete();
                        }
                    }

                    @Override
                    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int rCode) {

                    }
                });
                WeatherSearchQuery weatherLiveSearchQuery = new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE);
                weatherLiveSearch.setQuery(weatherLiveSearchQuery);
                weatherLiveSearch.searchWeatherAsyn();
            }
        })
                .subscribeOn(Schedulers.io())
                .doOnNext(new Consumer<LocalWeatherLive>() {
                    @Override
                    public void accept(LocalWeatherLive localWeatherLive) throws Exception {
                        MBLocationManager.this.localWeatherLive = localWeatherLive;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
    }