如何获取gps位置android

how to get gps location android(如何获取gps位置android)
本文介绍了如何获取gps位置android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拥有一个恒定的 gps 侦听器,它将每隔 x 分钟将其位置(经纬度坐标)发送到 Web 服务器.单击按钮时,它还将其位置发送到网络服务器.我意识到要获得 gps 信号,您需要输入多久才能找到一个位置,但是我如何编写一个程序来获取 gps 位置并每 x 分钟发送一次其坐标(即使在后台没有并且按下按钮?

I am trying to have a constant gps listener that will send its location (long and lat coordinates) to a web server every x mins. On a button click it will also send its location to the webserver. I realize that to get the gps signal you type in how often to find a position, but how do I write a program that can get the gps location and send its coordinates every x mins (even in the background when not and by a button press?

//在点击中

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, whatshouldIputhere?, 0, this);

public void onLocationChanged(Location location) {
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
    }
}

推荐答案

我已经搞定了:

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

这很简单.它获得了最好的可用提供商并获得了它最后一个已知的位置.
如果您只想使用 GPS,请尝试 这个.

希望对您有所帮助!

It's simple. It gets the best available provider and gets its last known position.
If you want it only with the GPS, try this.

Hope it helps!

试试这个:

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    LocationListener loc_listener = new LocationListener() {

        public void onLocationChanged(Location l) {}

        public void onProviderEnabled(String p) {}

        public void onProviderDisabled(String p) {}

        public void onStatusChanged(String p, int status, Bundle extras) {}
    };
    locationManager
            .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
    location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

此代码获取最后一个已知位置,然后请求实际位置.

This code gets the last known location and then do a request for the actual location.

这篇关于如何获取gps位置android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)