错误获取Android的定位(GPS和网络) [英] Error getting Location Android (GPS and Network)

查看:181
本文介绍了错误获取Android的定位(GPS和网络)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用下面的code让我的应用程序的位置:

 公共场所的getLocation(){

    布尔isGPSEnabled = FALSE;
    布尔isNetworkEnabled = FALSE;

    尝试 {

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

        如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled)返回NULL;

        LocationManager经理=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        位置= manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        如果((位置= NULL和放大器;!及(location.getTime()+ 60000< System.currentTimeMillis的()))||
                位置== NULL){
            位置= manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }

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

    返回的位置;
}
 

在我使用的Nexus 4它工作正常和位置的值。但是,当我执行相同的code在GALAXY S2的位置= NULL。

在调用函数我检查了GPS和网络都启用。

您将如何correcte这个功能呢?

在调用此函数我称它是:

 公共无效startLocation(){

    尝试 {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,本);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);

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

解决方案

如果该设备还没有找到一个位置,它会返回null。

我敢打赌,如果你打开​​谷歌地图,让它打开你的应用程序之前找到你,它会正常工作。

请参阅我的回答<一href="http://stackoverflow.com/questions/19365035/location-servise-gps-force-closed/19366773#19366773">Location servise GPS强制关闭。 这是如何实现的回调,让你尽快知道作为一个位置可用一个例子。

I want to get the location in my APP using the following code:

public Location getLocation() {     

    boolean isGPSEnabled     = false;
    boolean isNetworkEnabled = false;

    try {   

        // Getting GPS and Network status
        isGPSEnabled     = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) return null;

        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        location                = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if ((location != null && (location.getTime() + 60000 < System.currentTimeMillis())) || 
                location == null){
            location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }                           

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

    return location;
}

When I am using a Nexus 4 it works properly and location has a value. But when I am executing the same code in a Galaxy S2 location = null.

Before calling the function I have checked that GPS and Network are both enabled.

How would you correcte this function?

Before calling this function I call this one:

public void startLocation() {

    try {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);    

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

解决方案

If the device have not yet found a location, it will return null.

I bet if your open Google Maps and let it locate you before opening your app, it will work as expected.

See my answer to Location servise GPS Force closed. This is an example on how you can implement a callback to let you know as soon as a location is available.

这篇关于错误获取Android的定位(GPS和网络)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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