首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用服务中的requestLocationUpdates后访问纬度、经度值

调用服务中的requestLocationUpdates后访问纬度、经度值
EN

Stack Overflow用户
提问于 2015-08-28 11:33:17
回答 1查看 27关注 0票数 0

基于众包,我有TrackingService组件来跟踪公交在我的城市的位置。TrackingService在后台运行,然后将数据传输到服务器。后台TrackingService是在应用程序启动时在MainActivity中启动的。

我有另一个部件可以进入公共汽车的位置。当用户在MainActivity (作为过滤器)中选择所需的总线路由/s时,将启动此组件。然后,alarmManager从intentService类开始,从服务器检索数据到Map活动。

一切都适合我,但是当用户单击MainActivity中的按钮时,我想在总线位置旁边显示用户当前的位置。这意味着当用户单击onLocationChanged() in TrackingService类(TrackingService组件)中的按钮时,必须将计算出来的TrackingService检索到Map活动(访问组件)。

目前,我是在LocalBroadcastManager的帮助下这样做的,但在这种情况下,只有在调用lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,this);之后才会显示当前位置,这会导致对用户当前位置的可视化延迟,这对用户不友好。

当用户单击pLat,pLong中的按钮时,是否有任何方法可以长时间存储MainActivity的最后一个值?

TrackingService类:

代码语言:javascript
复制
public class TrackingService extends Service implements
        LocationListener {
    public double pLong;
    public double pLat;
    ...
        @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        detectLocation();
        return START_STICKY;
    }
    private void detectLocation() {
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 0,
                this);
    }
    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
        pLong = location.getLongitude();
        pLat = location.getLatitude();

        Intent intent = new Intent(Map.RECEIVE_latLng);
        intent.putExtra("location",location);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
           .....

     }  

}

地图活动:

代码语言:javascript
复制
    public class Map extends FragmentActivity implements OnMapReadyCallback   {
   public static final String RECEIVE_latLng = "com.bustracker.RECEIVE_latLng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

    }
        @Override
protected void onStart() {
    super.onStart();        
    LocalBroadcastManager bManager =    LocalBroadcastManager.getInstance(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(RECEIVE_latLng);
    bManager.registerReceiver(bReceiver, intentFilter);
}

    private BroadcastReceiver bReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(RECEIVE_latLng)) {
                              Location location = intent.getParcelableExtra("location");
             double lng = location.getLongitude();
             double lat = location.getLatitude();
             LatLng ll = new LatLng(lat, lng);
             MarkerOptions markerOpt = new MarkerOptions().title("My Location")
                        .position(ll);
             myLocatMarker = map.addMarker(markerOpt);
            }
          }
        };      
      }
EN

回答 1

Stack Overflow用户

发布于 2015-08-28 11:50:24

您可以在的onConnected方法中获得最后一个位置。参见开发人员文档中的以下内容:https://developer.android.com/training/location/retrieve-current.html

否则,您可以将用户的最后位置存储在共享首选项中,并在开始活动时检索该位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32270116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档