1

我想获得用户的地理位置。获得坐标后,我想根据该地理位置加载记录。如果有超过 1 个结果,则显示索引页,

LHW.Store = DS.Store.extend
  revision: 11
  adapter: DS.RESTAdapter

DS.RESTAdapter.reopen
  namespace: 'api'

LHW.Company = DS.Model.extend
  name: DS.attr 'string'
  address: DS.attr 'string'
  city: DS.attr 'string'

LHW.IndexRoute = Ember.Route.extend
  setupController: (controller, route) ->
    # get Geolocation
    _route = @
    navigator.geolocation.getCurrentPosition ((location) ->
      #success
      _route.geoLocation(location)
     ), (error) ->
      #error
      switch(error.code)
        when error.PERMISSION_DENIED then console.log "User denied the request for Geolocation."
        when error.POSITION_UNAVAILABLE then console.log "Location information is unavailable."
        when error.TIMEOUT then console.log "The request to get user location timed out."
        when error.UNKNOWN_ERROR then console.log "An unknown error occurred."

  geoLocation: (location) ->
    _route = @
    @get('store').findQuery('company',{longitude: location.coords.longitude, latitude: location.coords.latitude}).then ((data) =>
      _route.controllerFor('companies').set('content', data)
      if data.get('length') > 1
        _route.transitionTo('companies');

    ), (error) ->
      console.log "error"

LHW.CompaniesRoute = Ember.Route.extend()

LHW.IndexController = Ember.ArrayController.extend
  beforeModel: ->
    console.log 'before'

所以这就是我现在得到的。但这有几个问题

  1. 如果你去 /companies 而不是 / 它会跳过地理位置检查。但是,如果我将地理位置移动到公司路线,那么在获取地理位置时用户看到的页面是什么?
  2. 创建一个开关并检查路由中的长度,感觉有点脏,我认为 Companyroute 应该对此做出响应

所以这主要是一个流程问题,我无法解决,异步地理定位并没有让事情变得更容易......

4

0 回答 0