Android 按时间间隔发送纬度和经度,Location 对象

发布于 2024-12-08 17:48:39 字数 1235 浏览 1 评论 0原文

我看过其他一些问题和教程,但我没有得到我想要的东西

我正在尝试以这种格式获取纬度和经度

latitude = 40.769134 , longitude = -73.960905

我实现了这个课程,它需要我的全局变量纬度和经度(它们是长数据类型)

 class GpsListener implements LocationListener{

        public void onLocationChanged(Location location) {
              // TODO Auto-generated method stub
              latitude = location.getLatitude();
              longitude = location.getLongitude();
              //float speed = location.getSpeed();
              checkin();

        }

        public void onProviderDisabled(String provider) {
              // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
              // TODO Auto-generated method stub

        }


        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

}

我在这里查看了其他教程,并注意到人们使用 LocationManager 类与我的接口进行交互LocationListener 但我真正需要做的就是使用 Location 对象,因为我的类 GpsListener.onLocationChanged(.. ) 需要一个位置对象,但是您能否给出有关如何使用位置对象或如何简单地获取经度和纬度的示例或指导?

我只是将纬度和经度传递给服务器,因此我不需要对 GPS 模块执行任何其他操作,我的 checkin() 函数使服务器调用

I've looked at a few other questions and tutorials but I'm not getting what I'm looking for

I am trying to get the latitude and longitude in this format

latitude = 40.769134 , longitude = -73.960905

I implemented this class which takes my global variables latitude and longitude (they are long data types)

 class GpsListener implements LocationListener{

        public void onLocationChanged(Location location) {
              // TODO Auto-generated method stub
              latitude = location.getLatitude();
              longitude = location.getLongitude();
              //float speed = location.getSpeed();
              checkin();

        }

        public void onProviderDisabled(String provider) {
              // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
              // TODO Auto-generated method stub

        }


        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

}

I've looked at other tutorials here and notice that people use the LocationManager class to interface with my LocationListener but all I really need to do is use a Location object becuase my class GpsListener.onLocationChanged(.. ) needs a location object, but can you give an example or guidance on how to use the location object or how to simply get this longitude and latitude?

I am simply passing the latitude and longitude to a server so I dont need to do anything else with the GPS module, my checkin() function makes the server call

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2024-12-15 17:48:39

您是否试图避免使用 LocaitonManager ?

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,new MyLocationListener());
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

如果您只想使用一次,我想您可以在收到位置后取消注册所有内容。但也许您想确保精度在 x 米等范围内。

are you trying to avoid using the LocaitonManager?

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,new MyLocationListener());
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

if you just want to use it once, I suppose you could deregister everything after receiving a location. But maybe you want to make sure the accuracy is within x meters etc..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文