Android擷取手機經緯度(GPS、NetWork),androidgps

來源:互聯網
上載者:User

Android擷取手機經緯度(GPS、NetWork),androidgps

簡單封裝了一下,擷取簡單,看代碼:

import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;/** * 位置資訊的Utils *  * @author zsl * */public class LocationUtils {    // 緯度    public static double latitude = 0.0;    // 經度    public static double longitude = 0.0;    /**     * 初始化位置資訊     *      * @param context     */    public static void initLocation(Context context) {        LocationManager locationManager = (LocationManager) context                .getSystemService(Context.LOCATION_SERVICE);        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {            Location location = locationManager                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);            if (location != null) {                latitude = location.getLatitude();                longitude = location.getLongitude();            }        } else {            LocationListener locationListener = new LocationListener() {                // Provider的狀態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數                @Override                public void onStatusChanged(String provider, int status,                        Bundle extras) {                }                // Provider被enable時觸發此函數,比如GPS被開啟                @Override                public void onProviderEnabled(String provider) {                }                // Provider被disable時觸發此函數,比如GPS被關閉                @Override                public void onProviderDisabled(String provider) {                }                // 當座標改變時觸發此函數,如果Provider傳進相同的座標,它就不會被觸發                @Override                public void onLocationChanged(Location location) {                    if (location != null) {                    }                }            };            locationManager                    .requestLocationUpdates(LocationManager.NETWORK_PROVIDER,                            1000, 0, locationListener);            Location location = locationManager                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);            if (location != null) {                latitude = location.getLatitude(); // 經度                longitude = location.getLongitude(); // 緯度            }        }    }}
  • 使用
//初始化LocationUtils.initLocation(activity());//擷取經緯度Log.e("經度:"+LocationUtils.longitude);Log.e("緯度:"+LocationUtils.latitude);

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.