Android - 仅获取一次位置

发布于 2024-10-12 18:28:26 字数 171 浏览 2 评论 0原文

我需要获取 Android 应用程序上的当前用户位置,因此我在网上阅读了一些教程和示例,但我看到在所有示例中,位置都是从“onLocationChange”中检索的,这意味着每次位置改变时,执行“onLocationChange”中的代码。

我只需要在应用程序启动时获取用户位置。

感谢您的帮助!

i need to get the current user location on an android app, so i've read some tutorials and samples on the web, but i see that in all the examples, the location is retrived from a "onLocationChange" that mean that every time the location change, the code in the "onLocationChange" is executed.

i need only to get the user location at the moment the app is started.

Thanks for your help!

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

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

发布评论

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

评论(6

彩虹直至黑白 2024-10-19 18:28:26

您可以使用下面的代码获取最后知道的位置。它获取位置提供程序并向后循环数组。即从 GPS 开始,如果没有 GPS,则获取网络位置。当您需要获取位置时,您可以调用此方法。

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break                 out the loop*/
Location l = null;

for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}

double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}

You can get the last know location using the code below. It gets the location providers and loops over the array backwards. i.e starts with GPS, if no GPS then gets network location. You can call this method whenever you need to get the location.

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
List<String> providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break                 out the loop*/
Location l = null;

for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}

double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
海之角 2024-10-19 18:28:26

您可以使用 LocationManager 来执行此操作。getLastKnownLocation< /a>

You can do this with LocationManager.getLastKnownLocation

Bonjour°[大白 2024-10-19 18:28:26

将其放入主活动中:


boolean bFirst = true;
function void onCreate(blabla) {
  if(bFirst) {
    //Do your stuff to get the location
    bFirst = false;
  }
}

将相同的内容放入 onResume() 中;

Put this in the main activity:


boolean bFirst = true;
function void onCreate(blabla) {
  if(bFirst) {
    //Do your stuff to get the location
    bFirst = false;
  }
}

Put the same in the onResume();

梦断已成空 2024-10-19 18:28:26

为了使 Phobos 提议的 getGPS() 方法正常工作,您必须在 AndroidManifest.xml 中允许访问,

这将消除接收 0.0 的错误

添加这些行:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />

For the getGPS() method which Phobos proposed to work properly you have to allow access within your AndroidManifest.xml

That will get rid of your error of receiving 0.0

Add these lines:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.LOCATION_HARDWARE" />
梦回梦里 2024-10-19 18:28:26

您可以使用此方法从所有提供商处获取最佳位置。
首先创建一个名为FetchLocation的类,

public class FetchLocation implements LocationListener {
    private OnLocationFetchListner onLocationFetchListner;
    private LocationManager locationManager;

    public void setOnLocationFetchListner(OnLocationFetchListner fetchListner, Context context) {
        this.onLocationFetchListner = fetchListner;

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            onLocationFetchListner.OnFailed("PERMISSION DENIED");
            return;
        }
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        Log.d("TAG", "BestProvider: " + provider);
        if (provider != null) {
            locationManager.requestLocationUpdates(provider, 60 * 1000, 100, this, Looper.getMainLooper());
        } else {
            onLocationFetchListner.OnFailed("NO PROVIDERS");
        }
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        if (location != null) {
            onLocationFetchListner.OnComplete(location);
            locationManager.removeUpdates(this);
            locationManager = null;
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(@NonNull String provider) {

    }

    @Override
    public void onProviderDisabled(@NonNull String provider) {

    }
}

然后创建一个OnLocationFetchListner接口!

public interface OnLocationFetchListner {
    void OnComplete(Location location);
    void OnFailed(String e);
}

然后你可以在代码中的任何地方调用这个方法!

FetchLocation fetchLocation = new FetchLocation();
fetchLocation.setOnLocationFetchListner(new OnLocationFetchListner() {
    @Override
    public void OnComplete(Location location) {
        //do your work here
    }
}

    @Override
    public void OnFailed(String e) {
        Log.d("TAG", "OnFailed: "+e);
    }
}, context);

您必须将其添加到 AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

You can get the best location from all providers using this method.
First create a class which is called FetchLocation

public class FetchLocation implements LocationListener {
    private OnLocationFetchListner onLocationFetchListner;
    private LocationManager locationManager;

    public void setOnLocationFetchListner(OnLocationFetchListner fetchListner, Context context) {
        this.onLocationFetchListner = fetchListner;

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            onLocationFetchListner.OnFailed("PERMISSION DENIED");
            return;
        }
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        Log.d("TAG", "BestProvider: " + provider);
        if (provider != null) {
            locationManager.requestLocationUpdates(provider, 60 * 1000, 100, this, Looper.getMainLooper());
        } else {
            onLocationFetchListner.OnFailed("NO PROVIDERS");
        }
    }

    @Override
    public void onLocationChanged(@NonNull Location location) {
        if (location != null) {
            onLocationFetchListner.OnComplete(location);
            locationManager.removeUpdates(this);
            locationManager = null;
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(@NonNull String provider) {

    }

    @Override
    public void onProviderDisabled(@NonNull String provider) {

    }
}

Then you create a OnLocationFetchListner interface!

public interface OnLocationFetchListner {
    void OnComplete(Location location);
    void OnFailed(String e);
}

Then you can call this method anywhere in your code!

FetchLocation fetchLocation = new FetchLocation();
fetchLocation.setOnLocationFetchListner(new OnLocationFetchListner() {
    @Override
    public void OnComplete(Location location) {
        //do your work here
    }
}

    @Override
    public void OnFailed(String e) {
        Log.d("TAG", "OnFailed: "+e);
    }
}, context);

And the you have to add this in the AndroidManifest.xml

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