简体   繁体   English

使用Android获取GPS位置-并非始终有效

[英]get gps location with android - not always working

I'm working on an android app, the idea is that the user logs and sends a form to a database. 我正在开发一个android应用程序,其想法是用户登录并将表单发送到数据库。 This form also contains the actual position of the user using gps. 该表格还包含使用gps的用户的实际位置。

This is the code I'm using: 这是我正在使用的代码:

Oncreate -> Oncreate->

 LocationManager locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new MyLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, locationListener);

Then the class -> 然后上课->

class MyLocationListener implements LocationListener {


        @Override
        public void onLocationChanged(Location loc) {

            long variance;
            variance = age_minutes(loc);


            if (variance < 1) {

                Log.v("asdf","we are inside" + variance);

                posH.longitude = String.valueOf(loc.getLongitude());
                Log.v("asdf", posH.longitude);
                posH.latitude = String.valueOf(loc.getLatitude());
                Log.v("asdf", posH.latitude);

                posH.valid = true;

            } else {

                Log.v("asdf","we are outside" + variance);
                posH.valid = false;
            }


        }

The problem with this is that it doesn't always work: For example, I open the form, I press send but onlocationchanged is not called until I wait 20 seconds, sometimes more, sometimes is never called, doesn't matter if I move or not. 这个问题是,它并不总是有效:例如,我打开表单,按send键,但直到我等待20秒才调用onlocationchanged,有时甚至不调用,有时不调用,如果我移动或不。

I want a method that let me open the form, and automatically get the current gps position... 我想要一个让我打开表单并自动获取当前gps位置的方法...

You can use start with last know location until you get more current data. 您可以使用从最后一次知道的位置开始,直到获得更多当前数据为止。

Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

From Android Location Strategies : Android定位策略

Obtaining user location from a mobile device can be complicated. 从移动设备获取用户位置可能很复杂。 There are several reasons why a location reading (regardless of the source) can contain errors and be inaccurate. 位置读数(无论来源如何)可能包含错误且不准确,原因有多种。 Some sources of error in the user location include: 用户位置中的一些错误源包括:

  • Multitude of location sources GPS, Cell-ID, and Wi-Fi can each provide a clue to users location. 大量的定位源GPS,Cell-ID和Wi-Fi均可为用户定位提供线索。 Determining which to use and trust is a matter of trade-offs in accuracy, speed, and battery-efficiency. 确定要使用和信任的是权衡准确性,速度和电池效率的问题。

  • User movement Because the user location changes, you must account for movement by re-estimating user location every so often. 用户移动由于用户位置发生变化,因此您必须经常重新估算用户位置来解决移动问题。

  • Varying accuracy Location estimates coming from each location source are not consistent in their accuracy. 变化的准确性来自每个位置来源的位置估计的准确性不一致。 A location obtained 10 seconds ago from one source might be more accurate than the newest location from another or same source. 10秒钟前从一个来源获得的位置可能比从另一个或相同来源获得的最新位置更准确。 These problems can make it difficult to obtain a reliable user location reading. 这些问题可能使得难以获得可靠的用户位置读数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM