Ext JS:如何将地理位置值传递给url?

时间:2018-01-10 07:08:41

标签: javascript extjs geolocation openweathermap

我正在使用NjdhvExtJS-GeoLocation库。在样本用法上,他定义了一个打印当前地理位置值的函数;

//'MyApp.view.dash.DashController' where function defined.

  refreshGeoLocation : function( refresh ) {
            var className = '',
                geo;
            if (Ext.isClassic) {
                className = 'MyApp.utils.OoGeolocation';
            } else if (Ext.isModern) {
                className = 'Ext.util.Geolocation';
            }
            if (className) {
                geo = Ext.create(className, {
                    autoUpdate: false,
                    listeners: {
                        locationupdate: function (geo) {
                            console.log(geo);
                            console.log('Wait for it! Geolocation info is coming up!');
                            Ext.Msg.alert('Success', `New latitude: ${geo.getLatitude()} <br>New Longitude: ${geo.getLongitude()}`);
                            console.log(`New latitude: ${geo.getLatitude()} New Longitude: ${geo.getLongitude()}`);
                        },
                        locationerror: function (geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
                            if (bTimeout) {
                                Ext.Msg.alert('Erroe', 'Timeout occurred.');
                            } else {
                                Ext.Msg.alert('Erroe', 'Error occurred');
                            }
                        }
                    }
                });
                geo.updateLocation();
            } else {
                Ext.Msg.alert('Erroe', 'No class found');
            }
        }

如何将这些印刷值传递到proxy urlLATITUDELONGITUDE

Ext.define('MyApp.view.dash.WeatherData', {
    extend: 'Ext.DataView',
    xtype: 'weatherData',

    requires: [
        'Ext.data.reader.Json',
        'MyApp.utils.OoGeolocation', //The library using for Geolocation
        'MyApp.view.dash.DashController' //Where refreshGeolocation function defined
    ],

    baseCls: 'weather-panel',
    border: false,

    store: {
        fields: [
            {
                name: 'summary',
                mapping: 'weather[0].main'
            },
            {
                name: 'description',
                mapping: 'weather[0].description'
            }
        ],
        proxy: {
            type: 'jsonp',
            // url: 'http://api.openweathermap.org/data/2.5/weather?lat=36.90&lon=36.64&units=metric&appid=9b59049894d42af608baf69f869b9ace',
            url: 'http://api.openweathermap.org/data/2.5/weather?lat=' + LATITUDE +'&lon=' + LONGITUDE +'&units=metric&appid=9b5904989',
            reader: {
                type: 'json'
            }
        },
        autoLoad: true
    },

    itemTpl: '<div class="weather-image-container"><img src="../../../resources/img/ico_cloud.png" alt="{summary}"/></div>' +
                '<div class="weather-details-container">' +
                '<div>{main.temp}&#176;</div>' +
                '<div>{summary}</div>' +
                '<div>{description}</div>' +
             '</div>'
});

更新<!/强> 亲爱的@Njdhv这里是更新的片段;

Singleton类

Ext.define("MyApp.view.weather.WeatherUtil", {
    singleton: true,
    alternateClassName: 'weatherutil',
    config: {
        latitude: 0,
        longitude: 0
    },
    constructor: function (config) {
        this.initConfig(config);
    }
});

和DataView类

Ext.define('MyApp.view.weather.WeatherData', {
    extend: 'Ext.DataView',
    xtype: 'weatherdata',

    requires: [
        'Ext.data.reader.Json',
        'MyApp.utils.OoGeolocation', //Geolocation Library
        'MyApp.view.dash.WeatherUtil' //Singleton class
    ],
    ...
    proxy: {
            type: 'jsonp',
            url: `http://api.openweathermap.org/data/2.5/weather?lat=${weatherutil.getLatitude()}&lon=${weatherutil.getLongitude()}&units=metric&appid=9b590`,
            reader: {
                type: 'json'
            }
        },

这里是DashController for function of button;

refreshGeoLocation: function (button) {
        var store = button.up('#weatherView').down('weatherdata').getStore();

        store.load({
            url: `http://api.openweathermap.org/data/2.5/weather?lat=${weatherutil.getLatitude()}&lon=${weatherutil.getLongitude()}&units=metric&appid=9b59049894d42af608baf69f869b9ace`,
        });
        button.setDisabled(false);
    },

    getGeolocation : function( refresh ) {
        var className = '',
            geo;
        if (Ext.isClassic) {
            className = 'MyApp.utils.OoGeolocation';
        } else if (Ext.isModern) {
            className = 'Ext.util.Geolocation';
        }
        if (className) {
            geo = Ext.create(className, {
                autoUpdate: false,
                listeners: {
                    locationupdate: function (geo) {
                        console.log(geo);
                        console.log('Wait for it! Geolocation info is coming up!');
                        //Ext.Msg.alert('Success', `New latitude: ${geo.getLatitude()} <br>New Longitude: ${geo.getLongitude()}`);
                        console.log(`New latitude: ${geo.getLatitude()} New Longitude: ${geo.getLongitude()}`);
                    },
                    locationerror: function (geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
                        if (bTimeout) {
                            Ext.Msg.alert('Error', 'Timeout occurred.');
                        } else {
                            Ext.Msg.alert('Error', 'Error occurred');
                        }
                    }
                }
            });
            geo.updateLocation();
        } else {
            Ext.Msg.alert('Error', 'No class found');
        }
    },

和WeatherView在哪里创建按钮;

    Ext.define('MyApp.view.dash.WeatherView', {
    extend: 'Ext.panel.Panel',
    xtype: 'weatherview',

    requires: [
        'MyApp.view.weather.WeatherData',
        'MyApp.view.weather.WeatherWindow',
        'MyApp.view.weather.WeatherUtil'
    ],

    closable: false,
    resizable: false,
    title: 'Weather',
    itemId: 'weatherView',
    iconCls: 'x-fa fa-bell',
    height: 400,

    cls: 'quick-graph-panel shadow',
    layout: 'fit',

    tools: [
        {
            type: 'refresh',
            handler: 'refreshGeoLocation'
            // listeners: {
            //     click: 'refreshGeoLocation'
            // }
        },
        {
            type: 'gear',
            handler: 'weatherWindow'
        }
    ],
    // html: 'Welcome to our weather app. Click on refresh to get the latest weather information',

    items: [{
        xtype: 'weatherdata'
    }],

    listeners: {
        beforerender: 'getGeolocation'
    }
});

1 个答案:

答案 0 :(得分:1)

为此,您需要在应用程序中存储latitudelongitude。您可以使用 ExtJS singleton。只要您需要,此singleton可在整个应用程序中访问。

我已更新我的ExtJS-GeoLocation库,请参阅。

代码段

  

Singleton类示例

Ext.define("GeoLocation.util.CommonUtility", {
    singleton: true,
    alternateClassName: 'commonutility',
    config: {
        latitude: 0,
        longitude: 0
    },
    constructor: function (config) {
        this.initConfig(config);
    }
});
  

带有商店的数据视图示例

/**
 * This class is the main view for the application. It is specified in app.js as the
 * "mainView" property. That setting automatically applies the "viewport"
 * plugin causing this view to become the body element (i.e., the viewport).
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('GeoLocation.view.main.Main', {
    extend: 'Ext.tab.Panel',
    xtype: 'app-main',

    requires: [
        'Ext.plugin.Viewport',
        'Ext.window.MessageBox',
        'GeoLocation.view.main.MainController',
        'GeoLocation.util.Geolocation'
    ],
    controller: 'main',
    ui: 'navigation',
    //tabBarHeaderPosition: 1,
    titleRotation: 0,
    tabRotation: 0,
    activeTab: 0,
    items: [{
        title: 'Geo Location',
        iconCls: 'x-fa fa-map',
        itemId: 'geoLocation',
        items: [{
            xtype: 'button',
            text: 'Check weather',
            handler: 'onWeatherButtonClick'
        }, {
            xtype: 'dataview',
            store: {
                fields: [{
                    name: 'summary',
                    mapping: 'weather[0].main'
                }, {
                    name: 'description',
                    mapping: 'weather[0].description'
                }],
                proxy: {
                    type: 'jsonp',
                    url: `http://api.openweathermap.org/data/2.5/weather?lat=${commonutility.getLatitude()}&lon=${commonutility.getLongitude()}&units=metric&appid=9b59049894d42af608baf69f869b9ace`,
                    reader: {
                        type: 'json'
                    }
                },
                autoLoad: true
            },
            itemTpl: '<div class="weather-image-container"><img src="../../../resources/img/ico_cloud.png" alt="{summary}"/></div>' +
                '<div class="weather-details-container">' +
                '<div>{main.temp}&#176;</div>' +
                '<div>{summary}</div>' +
                '<div>{description}</div>' +
                '</div>'
        }]
    }],
    listeners: {
        beforerender: 'onMainViewRender'
    }
});