从OnLocationChange侦听器获取太多延迟。? [英] Getting too much delay from OnLocationChange listener.?

查看:132
本文介绍了从OnLocationChange侦听器获取太多延迟。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在基于Geolocation开发一个Android项目。我已经集成了 FUSE库。但最初的纬度,经度值变为零。从 OnLocationChange侦听器中获取太多延迟。任何人都可以提出一个适当的解决方案来获取正确的GEO位置值提前致谢。

解决方案

public class GPSTracker extends Service implements LocationListener {
private final Context mContext ;
boolean isGPSEnabled = false; // GPS状态标志
boolean isNetworkEnabled = false; //网络状态标志
boolean canGetLocation = false; // GPS状态标志

位置位置; //位置
双纬度; //纬度
双经度; // longitude

//改变的最小距离以米为单位的更新
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10米

//以毫秒为单位的最小更新时间间隔
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟

//声明位置管理器
受保护的LocationManager locationManager;

公共GPSTracker(上下文环境){
this.mContext = context;
getLocation();
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//如果启用,您需要做什么...
getLocation();
} else {
//如果未启用,请执行以下操作...
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle(GPS is settings); //设置对话框标题

//设置对话框消息
alertDialog.setMessage(GPS未启用,是否要进入设置菜单?);

//按设置按钮
alertDialog.setPositiveButton(Settings,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int which){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});

//按下取消按钮
alertDialog.setNegativeButton(取消,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int其中){
dialog.cancel();
}
});

alertDialog.show(); //显示提示消息



public Location getLocation(){
try {
locationManager =(LocationManager)mContext
。 getSystemService(LOCATION_SERVICE);

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

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

if(!isGPSEnabled&&!isNetworkEnabled){
System.out.println(GPS Disabled);
//未启用网络提供商
}其他{
this.canGetLocation = true;
if(isNetworkEnabled){
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(网络,网络);
if(locationManager!= null){
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();


$ b //如果GPS启用使用GPS服务获得经纬度
if(isGPSEnabled){
if(location == null ){
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(已启用GPS,已启用GPS);
if(locationManager!= null){
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();





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

返回地点;

$ b $ **
*停止使用GPS监听器
*调用此函数将停止在您的应用中使用GPS
* * /
public void stopUsingGPS(){
if(locationManager!= null){
locationManager.removeUpdates(GPSTracker.this);


$ b $ ** b $ b *函数获得纬度
* * /
public double getLatitude(){
if(location!= null){
latitude = location.getLatitude();
}

//返回纬度
返回纬度;

$ b / **
*获得经度的函数
* * /
public double getLongitude(){
if(location! = null){
longitude = location.getLongitude();
}

//返回经度
返回经度;
}
$ b $ **
*检查GPS / wifi的功能
* @return boolean
* * /
public boolean canGetLocation (){
return this.canGetLocation;
}

/ **
*显示设置提示对话框的功能
*按下设置按钮后,将显示设置选项
* * /
public void onLocationChanged(Location location){
if(location!= null){
latitude = location.getLatitude();


$ b $ public void onProviderDisabled(String provider){} $ b $ public void onProviderEnabled(String provider){} $ b $ public void onStatusChanged(String provider ,int status,Bundle extras){}
@Override
public IBinder onBind(Intent arg0){return null;}
public void getLocationname(){}
}


I am currently working on a android project based on Geolocation. I have integrated FUSE library. But initially the latitude, longitude value getting zero. And getting too much delay from OnLocationChange listener. Can anyone suggest a proper solution for getting correct GEO location values. Thanks in advance.

解决方案

public class GPSTracker extends Service implements LocationListener {
    private final Context mContext;
    boolean isGPSEnabled = false;      // flag for GPS status
    boolean isNetworkEnabled = false;  // flag for network status
    boolean canGetLocation = false;    // flag for GPS status

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            //Do what you need if enabled...
            getLocation();
        } else {
            //Do what you need if not enabled...
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
            alertDialog.setTitle("GPS is settings"); // Setting Dialog Title

            // Setting Dialog Message
            alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

            // On pressing Settings button
            alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);
                }
            });

            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                }
            });

            alertDialog.show(); // Showing Alert Message
        }
    }

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

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

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

            if (!isGPSEnabled && !isNetworkEnabled) {
                System.out.println("GPS Disabled");
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    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);
                        Log.d("GPS Enabled", "GPS Enabled");
                        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(GPSTracker.this);
        }       
    }

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

        // return latitude
        return latitude;
    }

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

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     * */
    public void onLocationChanged(Location location) {
        if(location != null){
            latitude = location.getLatitude();
        }
    }

    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
    @Override
    public IBinder onBind(Intent arg0) {return null;}
    public void getLocationname() {}
}

这篇关于从OnLocationChange侦听器获取太多延迟。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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