如何获得在android的当前确切位置没有互联网连接? [英] How to get current exact location in android without internet connection?

查看:109
本文介绍了如何获得在android的当前确切位置没有互联网连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在当前的Andr​​oid确切位置坐标。我能够得到的位置,如果移动终端连接到无线互联网连接,但在其他情况下,当我去一些别的地方,没有互联网连接可用,那么它不会给当前的位置坐标。而不是给最后的位置坐标。

例如:

目前,我在一个城市,并通过WiFi连接获得当前坐标和移动到城市B,那么我不能获取当前坐标。取而代之的是它给人最后的位置坐标。

任何人都可以建议我怎么能得到当前的位置没有互联网连接。

 公共场所的getLocation(){
    尝试 {
        locationManager =(LocationManager)context.getSystemService(LOCATION_SERVICE);

        //获取GPS状态
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        //获取网络状态
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
            //没有网络提供商启用
        } 其他 {
            this.canGetLocation = TRUE;
            //首先从网络提供商处获取位置
            如果(isNetworkEnabled){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                如果(locationManager!= NULL){
                    位置= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    如果(位置!= NULL){
                        纬度= location.getLatitude();
                        经度= location.getLongitude();
                    }
                }
            }

            //如果GPS功能的获得纬度/经度使用GPS服务
            如果(isGPSEnabled){
                如果(位置== NULL){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                    如果(locationManager!= NULL){
                        位置= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        如果(位置!= NULL){
                            纬度= location.getLatitude();
                            经度= location.getLongitude();
                        }
                    }
                }
            }
        }

    }赶上(例外五){
        e.printStackTrace();
    }

    返回的位置;
}

/ **
 *停止使用GPS监听器
 *调用此函数将停止使用GPS在你的应用程序
 * * /
公共无效stopUsingGPS(){
    如果(locationManager!= NULL){
        locationManager.removeUpdates(GetLocations.this);
    }
}

/ **
 *功能得到纬度
 * * /
公共双getLatitude(){
    如果(位置!= NULL){
        纬度= location.getLatitude();
    }
    返回纬度;
}

/ **
 *功能得到经度
 * * /
公共双getLongitude(){
    如果(位置!= NULL){
        经度= location.getLongitude();
    }
    返回经度;
}
 

解决方案

在code你已经显示仅仅是的getLocation 类的方法。 presumably类也实现 LocationListener的,正确吗? (假设它确实没有看到它,因为你将本,在 requestLocationUpdates 作为监听器。)

您已经表明该方法将只返回最近的已知位置,因为通过供应商的位置更新的同步。你表示要通过注册一个监听器得到的位置信息,而监听器定期得到更新(根据你给它的设置,并根据实际设备的能力和情况)。注册后,你不会得到的位置立即回。

在你的问题的情况下,获取位置信息,而没有连接到网络,你需要使用GPS提供商(它的工作原理,而不是连接到数据网络)。一旦你注册了一个GPS提供商监听器(因为你已经有了),你会得到通过在听者的回调位置更新, onLocationChanged(地点位置)

什么是您的 onLocationChanged 回调呢?

这是在那里你会得到更新的位置信息,几秒钟到几分钟您注册GPS提供商(多久取决于你正在使用的设备和环境条件的硬件后,GPS使用多颗卫星,并能快速地与天空的一个明确的说法,它可能无法正常工作,如果你是在室内或信号,否则受阻的话)。

在除了等待监听器让你更新的位置信息,你也可以考虑使用一些简单的,独立的,方法来获取最后的已知位置,并报名参加最佳供应商。你可以使用一些简单的循环和标准您有兴趣(粗VS精细,LOW_POWER与否,等等)。

Android的开发者已经在文档记录在本pretty的好,而Protips的位置开放源代码示例应用程序,以及深度潜水的博客文章(其中有在它链接到所有其他资源) : http://android-developers.blogspot.com/ 2011/06 /深潜 - 到 - location.html

I want to get current exact location coordinates in android. I am able to get the location if mobile is connected to Wifi internet connection and but in other case when I go some where else and no internet connection available then it not giving the current location coordinates. instead giving last location coordinates.

For example:

Currently I am in A city and get current coordinates by on the wifi connection and move to City B then I am not able to fetch current coordinates. Instead of this its giving last location coordinates.

Can anyone suggest me how can I get current location without internet connection.

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }

            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GetLocations.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }
    return longitude;
}

解决方案

The code you've show is just the getLocation method of your class. Presumably your class also implements LocationListener, correct? (Assuming it does without seeing it, because you assign "this" in requestLocationUpdates as the listener.)

The method you have shown will only ever return the LAST KNOWN location because location updates via providers are asynchronous. You indicate that you want to get location info via registering a listener, and that listener gets updates periodically (based on the settings you give it, and based on actual device capabilities and circumstances). You will NOT get the location back immediately after registering.

In the case of your question, getting location information while NOT connected to the network, you'll need to use the GPS provider (it works while not connected to the data network). Once you've registered a GPS provider listener (as you already have), you will get location updates via the callback in the listener, onLocationChanged(Location location).

What does your onLocationChanged callback do?

THAT is where you'll get updated location information a few seconds to minutes after you register the GPS provider (how soon depends on the hardware in the device you're using and on environmental conditions, GPS uses multiple satellites and works faster with a clear view of the sky, it may not work at all if you are indoors or the signal is otherwise obstructed).

In addition to waiting for the listener to get you updated location information you may also want to consider using some simpler, separate, methods to get the last known location and to register for the best provider. You can use a few simple loops and the Criteria you are interested in (COARSE vs FINE, LOW_POWER or not, etc).

The Android devs have documented this pretty well in the docs, and the "Protips for Location" open source sample app, and the "Deep Dive" blog post (which has links to all the other resources within it): http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html.

这篇关于如何获得在android的当前确切位置没有互联网连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆