0

我正在尝试将设备的当前位置添加到代理调用中。我已经添加了带有“+radius+”和“+values+”的变量。我使用了 Ext.util.Geolocation 并且可以在控制台中看到地理位置。我如何从中获得 lat 和 long 变量,以便我可以添加到代理中,例如 '+lat+' 和 '+lon+'

Ext.define('FirstApp.controller.History', {
extend:'Ext.app.Controller',

config:{
    refs:{
        main:'main',
        placesContainer:'placesContainer',
        placesList:'placesContainer places list',
        info:'info',
        review:'review',
        rss:'rss',
        favourites:'favourites',
        mapcontainer:'mapcontainer',
        visit: '#visit',
        radius: '#radius'
    },
    control:{
        'placesContainer places list':{
            itemtap:'recordDetailsNavigation'
        },
        'main':{
            activeitemchange:'recordTabChanged'
        },
         '#home': {
            // On the tap event, call onNewTap
            tap: 'onHomeTap'
        },
          '#info': {
            // On the tap event, call onNewTap
            tap: 'onInfoTap'
        }
        ,
          '#search': {
            // On the tap event, call onNewTap
            tap: 'onSearchTap'
        }
          ,
          '#leaveReview': {
            // On the tap event, call onNewTap
            tap: 'onReviewTap'
        }

    },
    routes: {
        'placesContainer': 'gotoPlacesContainer',
        'placesContainer/:id': 'gotoPlacesContainerDetails',
        'info': 'gotoInfo',
        'review': 'gotoReview',
         'rss': 'gotoRss',
        'favourites': 'gotoFavourites',
        'mapcontainer': 'gotoMapContainer'
    }
     },
     recordDetailsNavigation:function (list, index, target, record) {
    console.log("recordDetailsNavigation");
    this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {
        url:'placesContainer/' + record.get('id')
    }),true);
    },
    recordTabChanged:function(tab,value, oldValue){
    console.log("recordTabChanged "+value.xtype);
    this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {
        url:value.xtype
    }),true);
     },


gotoPlacesContainer:function(){
    console.log('Goto PlacesContainer');
    this.getMain().setActiveItem(0);
    var items = this.getPlacesContainer().getItems();
    console.log("PlacesContainer has "+items.length+" children")
    if(items.length>1){
        this.getPlacesContainer().pop();
    }


},

gotoReview:function(){
    console.log('Goto Review');
    this.getMain().setActiveItem(1);
     Reviews.load();
},


gotoMapContainer:function(){
    console.log('Goto MapContainer');
    this.getMain().setActiveItem(2);
},

gotoPlacesContainerDetails:function(id){
    console.log('Goto PlacesContainer Details for id '+id);

    this.gotoPlacesContainer();
    this.getPlacesList().refresh();
    this.getPlacesList().on('refresh',function(){
        var store = Ext.getStore('Places');
        var index = store.find('id',id);
        var record = store.getAt(index)
        this.getPlacesContainer().push({
            xtype:'details',
            title:record.data.name,
            data:record.data
        })
        this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {
            url:'placesContainer/' + record.get('id')
        }),true);
    },this);

},

  gotoInfo:function(){
    console.log('Goto Info');
    this.getMain().setActiveItem(3);
},

    gotoRss:function(){
    console.log('Goto Rss');
    this.getMain().setActiveItem(4);
},


gotoFavourites:function(){
    console.log('Goto Favourites');
    this.getMain().setActiveItem(5);
},


 onHomeTap: function() {

    Ext.Viewport.setActiveItem(Ext.create('FirstApp.view.Home'));
},

 onReviewTap: function() {

    Ext.Viewport.setActiveItem(Ext.create('FirstApp.view.CreateReview'));
},

  onInfoTap: function() {
    Ext.Viewport.setActiveItem(Ext.create('FirstApp.view.Info'));
},

  onSearchTap: function() {

    Ext.Viewport.setActiveItem(Ext.create('FirstApp.view.Main'));

    var values = this.getVisit().getValue();
     var radius = this.getRadius().getValue() * 1000;

    console.log(values);
    console.log(radius);

    var proxy = {
    type:'ajax',
    id:'myPlaceProxy',
    url:'https://maps.googleapis.com/maps/api/place/search/json?    location=52.247983,-7.141113&radius='+radius+'&types='+values+'&name=&sensor=false&key=',
    reader:{
        type:'json',
        rootProperty:'results'
    }
    }


   var geo = new Ext.util.Geolocation({
    autoUpdate: false,
    allowHighAccuracy: true,
    listeners: {
    locationupdate: function(geo) {
       lat = geo.getLatitude();
       lon = geo.getLongitude();
       var proxy = Ext.getCmp('myPlaceProxy');
       proxy.setUrl('https://maps.googleapis.com/maps/api/place/search/json?location='+lat+','+lon+'&radius='+radius+'&types='+values+'&name=&sensor=false&key=AIzaSyAAStZvDJ_5ENODEdSCanLWgJBG6p6eXBQ');
       console.log(proxy);
       Ext.StoreMgr.get('Places').setProxy(proxy);
       Ext.StoreMgr.get('Places').load();    
    }
    }
   });

  geo.updateLocation();
  }


});


Ext.define('FirstApp.store.Places',{
extend:'Ext.data.Store',

config:{

    autoLoad:false,
    model:'FirstApp.model.Place',
    proxy:{


    }
}
})
4

1 回答 1

0

首先将 id 提供给您的代理,以便我们可以随时通过 id 访问它

var proxy = {
        type:'ajax',
        id:'myPlaceProxy',
        url:'https://maps.googleapis.com/maps/api/place/search/json?location=52.247983,-7.141113&radius='+radius+'&types='+values+'&name=&sensor=false&key=',
        reader:{
            type:'json',
            rootProperty:'results'
        }
    }

然后在您的地理位置的 locationupdate 事件中访问代理

var geo = new Ext.util.Geolocation({
    autoUpdate: true,
    allowHighAccuracy: true,
    listeners: {
        locationupdate: function(geo) {
           lat = geo.getLatitude();
           lon = geo.getLongitude();
           var proxy = Ext.getCmp('myPlaceProxy');
           proxy.setUrl('https://maps.googleapis.com/maps/api/place/search/json?location='+lat+','+lon+'&radius='+radius+'&types='+values+'&name=&sensor=false&key='');
           console.log(proxy);
           Ext.StoreMgr.get('Places').setProxy(proxy);
           Ext.StoreMgr.get('Places').load();    
        }
    }
});

然后随时通过调用此命令触发您的地理位置的 locationupdate 事件

geo.updateLocation();

编辑:

但我强烈建议不要总是创建 var geo 你只需要调用这个 geo.updateLocation(); 一旦你在全球范围内创建它,你就可以使用它,这样你就可以更好地管理它

试试这个:

Ext.define('FirstApp.store.Places',{
extend:'Ext.data.Store',
id: 'myPlaces',
config:{

    autoLoad:false,
    model:'FirstApp.model.Place',
    proxy:{
        type:'ajax',
        url:'https://maps.googleapis.com/maps/api/place/search/json?    location=52.247983,-7.141113&radius='+radius+'&types='+values+'&name=&sensor=false&key=AIzaSyAAStZvDJ_5ENODEdSCanLWgJBG6p6eXBQ',
        reader:{
        type:'json',
        rootProperty:'results'
    }
}
});

之后,您可以将点击事件中的 url 设置为

onSearchTap: function() {

    Ext.Viewport.setActiveItem(Ext.create('FirstApp.view.Main'));

    var values = this.getVisit().getValue();
     var radius = this.getRadius().getValue() * 1000;

    console.log(values);
    console.log(radius);    

   var geo = new Ext.util.Geolocation({
    autoUpdate: false,
    allowHighAccuracy: true,
    listeners: {
    locationupdate: function(geo) {
       lat = geo.getLatitude();
       lon = geo.getLongitude();
       Ext.StoreMgr.get('Places').getProxy().setUrl('https://maps.googleapis.com/maps/api/place/search/json?location='+lat+','+lon+'&radius='+radius+'&types='+values+'&name=&sensor=false&key='');
       Ext.StoreMgr.get('Places').load();    
    }
    }
   });

  geo.updateLocation();
  }
于 2013-03-24T09:33:46.530 回答