充分利用GPS当前位置 [英] Getting current location from GPS

查看:198
本文介绍了充分利用GPS当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Criteria.ACCURACY_COARSE 我得到的位置,但它是不准确的。我想用GPS Criteria.ACCURACY_FINE onLocationChanged(地点)方法不叫。结果
我做了一些研究,但不能想通为什么。结果
有很多在计算器上类似的问题,但不幸的是我无法找到一个清楚我的问题。结果
下面是我的源$ C ​​$ C:

Using Criteria.ACCURACY_COARSE I get the location but it is not accurate. I wanted to use GPS Criteria.ACCURACY_FINE but the onLocationChanged(Location location) method is not called.
I did some researches but could not figured why.
There are lots of similar questions on StackOverflow but unfortunately I could not find one that clear my issue.
Below is my source code:

private void locationInformationSettings(){
    Criteria criteria= new  Criteria();;
    criteria.setAccuracy(Criteria.ACCURACY_FINE); //using Criteria.ACCURACY_COARSE works
    //criteria.setPowerRequirement(Criteria.POWER_LOW);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    String providername = locationManager.getBestProvider(criteria, true);

    myLocationListener = new MyLocationListener();

    locationManager.requestLocationUpdates(
            providername, //LocationManager.GPS_PROVIDER, 
            Utils.MINIMUM_TIME_BETWEEN_UPDATES, 
            Utils.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            myLocationListener
    );      
}

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {

        locLongitude = location.getLongitude();
        locLatitude = location.getLatitude();
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                locLongitude, locLatitude);
        Toast.makeText(RecordActivity.this, message, Toast.LENGTH_LONG).show();
        Log.e(TAG, "Location information received:(" + locLongitude + "," + locLatitude +")");
        locationManager.removeUpdates(myLocationListener);
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(RecordActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(RecordActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(RecordActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}

清单文件权限:

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

使用网络提供作品和GPS提供商不工作,为什么?结果
我检查我的设备的设置GPS服务,并在Android 4.3和Android 2.3.3测试。
结果
在此先感谢

Why using network provider works and GPS provider doesn't work?
I have checked the GPS service on my devices settings and testing on Android 4.3 and Android 2.3.3.
Thanks in advance

推荐答案

在这里你去

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

public class LocationManagerStatus extends Activity {

private LocationManager locationManager;
private TextView textView;
private final LocationListener gpsLocationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "GPS available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "GPS out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt + "GPS temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        locationManager.removeUpdates(networkLocationListener);
        textView.setText(textView.getText().toString()
                + "New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};
private final LocationListener networkLocationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        case LocationProvider.AVAILABLE:
            textView.setText(tvTxt + "Network location available again\n");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "Network location out of service\n");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt
                    + "Network location temporarily unavailable\n");
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Enabled\n");
    }

    @Override
    public void onProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Disabled\n");
    }

    @Override
    public void onLocationChanged(Location location) {
        textView.setText(textView.getText().toString()
                + "New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textview);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 5000, 0,
            networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000, 0, gpsLocationListener);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(networkLocationListener);
    locationManager.removeUpdates(gpsLocationListener);
}
}

这篇关于充分利用GPS当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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