Android gps定位

來源:互聯網
上載者:User

標籤:

我去,好久對沒來os來了編輯器都有三種風格了,這愉快的周末即將開始,下午了,來個gps是如何定位的,Android版本的,ios的請繞走。

首先還是需要引入你的許可權,在AndroidManifest.xml裡加上這幾個<!--網路 精確定位gps,許可權不懂的Google或者看API--><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

許可權都加上了,看看我的activity

package*********import***********public class LocationActivity extends Activity {    private String TAG = "LocationActivity";    private  LocationManager locationManager;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);    }    @Override    protected void onResume() {        super.onResume();        initData();//init    }    private void initData(){        if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {            Log.i(TAG,"gps close");                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);            startActivityForResult(intent, 0);            return;        } else {            Log.i(TAG,"gps open");        }        String bestProvider = locationManager.getBestProvider(getCriteria(),true);        Location location = locationManager.getLastKnownLocation(bestProvider);        updateView(location);        /*            1000毫秒  0米位移量,更新,定位方式我用的NETWORK_PROVIDER,            也可以用GPS_PROVIDER,只是gps在室內好像定不了        */        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,        1000, 0, locationListener);    }    private void updateGps(Location location) {        if (location != null) {                     Log.i(TAG,"裝置位置資訊,經度:"+String.valueOf(location.getLongitude())                        + "緯度:" + String.valueOf(location.getLatitude())                        + "海拔:" + String.valueOf(location.getAltitude()));        } else {            Log.i(TAG,"暫時未擷取到位置");        }    }    private Criteria getCriteria() {        Criteria criteria = new Criteria();        /*        設定定位精確度 Criteria.ACCURACY_COARSE比較粗略,        Criteria.ACCURACY_FINE則比較精細,此處注意,        如果想粗略的定位要去AndroidManifest.xml中加上粗略的許可權,        */         criteria.setAccuracy(Criteria.ACCURACY_FINE);        // 設定是否要求速度        criteria.setSpeedRequired(false);        // 設定是否允許電訊廠商收費        criteria.setCostAllowed(false);        // 設定是否需要方位資訊        criteria.setBearingRequired(false);        // 設定是否需要海拔資訊        criteria.setAltitudeRequired(true);        // 設定對電源的需求        criteria.setPowerRequirement(Criteria.POWER_LOW);        return criteria;    }    private LocationListener locationListener = new LocationListener() {            /**         * 位置資訊變化時觸發         */        public void onLocationChanged(Location location) {            updateGps(location);                    }            /**         * GPS狀態變化時觸發         */        public void onStatusChanged(String provider, int status, Bundle extras) {            switch (status) {                // GPS狀態為可見時                case LocationProvider.AVAILABLE:                    Log.i(TAG, "當前GPS狀態為可見狀態");                    break;                // GPS狀態為服務區外時                case LocationProvider.OUT_OF_SERVICE:                    Log.i(TAG, "當前GPS狀態為服務區外狀態");                    break;                // GPS狀態為暫停服務時                case LocationProvider.TEMPORARILY_UNAVAILABLE:                    Log.i(TAG, "當前GPS狀態為暫停服務狀態");                    break;            }        }            /**         * GPS開啟時觸發         */        public void onProviderEnabled(String provider) {            Location location = locationManager.getLastKnownLocation(provider);            updateGps(location);        }            /**         * GPS禁用時觸發         */        public void onProviderDisabled(String provider) {            updateGps(null);        }        };        private GpsStatus.Listener listener = new GpsStatus.Listener() {        @Override        public void onGpsStatusChanged(int event) {            switch (event) {                case GpsStatus.GPS_EVENT_FIRST_FIX:                    Log.i(TAG, "第一次定位");                    break;                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:                    Log.i(TAG, "衛星狀態改變");                    GpsStatus gpsStatus = locationManager.getGpsStatus(null);                    int maxSatellites = gpsStatus.getMaxSatellites();                    Iterator<GpsSatellite> iters = gpsStatus.getSatellites()                            .iterator();                    int count = 0;                    while (iters.hasNext() && count <= maxSatellites) {                        GpsSatellite s = iters.next();                        count++;                    }                    break;                case GpsStatus.GPS_EVENT_STARTED:                    Log.i(TAG, "定位啟動");                    break;                case GpsStatus.GPS_EVENT_STOPPED:                    Log.i(TAG, "定位結束");                    break;            }        }    };}

複製到你類中,有需要import的全部匯入即可,運行下查看是否可以擷取gps,親測三星是可以的,比較倉促,不足之處留言。

Android gps定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.