由于我们的应用程序的工作方式,我需要同步获取用户的当前位置。我们目前的实现使用
伪代码
com.google.android.gms.location.LocationListener
接收位置更新。问题是,在尝试第一次调用之前,我需要在服务器端更新位置,否则用户会得到错误的数据。
LocationServices.FusedLocationApi.getLastLocation()
不适用于此,因为 a) 它似乎总是返回 null(出于某种原因),并且 b) 我们已经将用户的最后已知位置保存在服务器端,因此检索最后已知位置并将其发送到服务器是多余的。伪代码
//this is the first call I need to make
webservice.appStart(AppStartBody body);
//then I want to retrieve the current location and send it to the server
webservice.setGeoLocation(getCurrentLocation());
//finally, I retrieve the items on the server based on the location
webservice.getListItems();
等待第一个位置更新事件是我想要避免的可能性,因为我不知道它会多快触发,而且我可能需要让我的用户在加载屏幕上等待很长时间,最终导致失去他们,因为没有人喜欢等待。
getLastLocation()
在开发者的测试手机上可能会返回null
,因为没有其他应用程序请求位置信息。在实际用户使用带有Facebook、Google Now等应用的手机上可能会好一些,但我们当然不能依赖于此。 - Markus Kauppinen