2015年11月30日 星期一

Android - 目前GPS位置

參考網址:
http://cw1057.blogspot.tw/2011/11/android-locationmanager.html

我不太的寫範例,
只是會希望能夠快速開發,
所以這裡貼的程式碼,會是複製後馬上就能進行
是我改寫了文章內容後的結果


public class LocationActivity extends Activity {
private LocationManager locationMgr;
private double longitude = 0;
private double latitude = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.locationMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE);
}


@Override
protected void onResume() {
super.onResume();

// 取得位置提供者,不下條件,讓系統決定最適用者,true 表示生效的 provider
String provider = this.locationMgr.getBestProvider(new Criteria(), true);

if (provider == null) {
debug_log("沒有 location provider 可以使用");
return;
}

debug_log("取得 provider - " + provider);
debug_log("requestLocationUpdates...");

// 註冊 listener,兩個 0 不適合在實際環境使用,太耗電
this.locationMgr.requestLocationUpdates(provider, 0, 0, mLocationListener);

debug_log("getLastKnownLocation...");
Location location = this.locationMgr.getLastKnownLocation(provider);
if (location == null) {
debug_log("未取過 location");
return;
}
debug_log("取得上次的 location");
mLocationListener.onLocationChanged(location);
}


public LocationListener mLocationListener = new LocationListener() {

@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();

debug_log("onLocationChanged...");
String msg = "經度: " + location.getLongitude() + ", 緯度: "
+ location.getLatitude();
debug_log(msg);
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
debug_log("onStatusChanged...");
// 當 location provider 改變時
}

@Override
public void onProviderEnabled(String provider) {
debug_log("onProviderEnabled...");
// 當 location provider 有效時
}

@Override
public void onProviderDisabled(String provider) {
debug_log("onProviderDisabled...");
// 當 location provider 無效時
}
};

private void debug_log(String msg) {
Log.e("log",msg);
}

@Override
protected void onPause() {
super.onPause();
this.debug_log("removeUpdates...");
this.locationMgr.removeUpdates(mLocationListener);
}
}

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


後來我改寫成一個Service了。
public class LocationService {
    private LocationManager locationMgr;
    private double longitude = 0;
    private double latitude = 0;
    private Context mContext;

    public LocationService(Context context){
        mContext = context;
        this.locationMgr = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
    }

    public double getLongitude(){
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void onResume() {
        // 取得位置提供者,不下條件,讓系統決定最適用者,true 表示生效的 provider        String provider = this.locationMgr.getBestProvider(new Criteria(), true);

        if (provider == null) {
            debug_log("沒有 location provider 可以使用");
            return;
        }

        debug_log("取得 provider - " + provider);
        debug_log("requestLocationUpdates...");

        // 註冊 listener,兩個 0 不適合在實際環境使用,太耗電        this.locationMgr.requestLocationUpdates(provider, 0, 0, mLocationListener);

        debug_log("getLastKnownLocation...");
        Location location = this.locationMgr.getLastKnownLocation(provider);
        if (location == null) {
            debug_log("未取過 location");
            return;
        }
        debug_log("取得上次的 location");
        mLocationListener.onLocationChanged(location);
    }

    public LocationListener mLocationListener = new LocationListener() {
        @Override        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();

            debug_log("onLocationChanged...");
            String msg = "經度: " + location.getLongitude() + ", 緯度: "                    + location.getLatitude();
            debug_log(msg);
        }

        @Override        public void onStatusChanged(String provider, int status, Bundle extras) {
            debug_log("onStatusChanged...");
            // 當 location provider 改變時        }

        @Override        public void onProviderEnabled(String provider) {
            debug_log("onProviderEnabled...");
            // 當 location provider 有效時        }

        @Override        public void onProviderDisabled(String provider) {
            debug_log("onProviderDisabled...");
            // 當 location provider 無效時        }
    };

    private void debug_log(String msg) {
        Log.e("log", msg);
    }

    public void onPause() {
        this.debug_log("removeUpdates...");
        this.locationMgr.removeUpdates(mLocationListener);
    }
}

------------------
最後發現,市內比較不好得到GPS座標,
使用getBestProvider,通常只能得到GPS Provider
而在市場這樣的BestProvider,就無法得到座標位置。

所以最後改寫如下
public void onResume() {
    requestLocationUpdates();
    Location location = getLocation();

    if (location == null) {
        debug_log("未取得 location");
        return;
    }
    debug_log("取得上次的 location");
    mLocationListener.onLocationChanged(location);
}

public void requestLocationUpdates() {
    if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
    }

    if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
    }
}

public Location getLocation() {
    debug_log("getLastKnownLocation...");
    Location location = null ;
    if (location ==null && mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        debug_log("get from gps " + location);
    }

    if (location ==null && mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        debug_log("get from network" + location);
    }

    return location;
}

以增加快速取得座標的機率

--------------
判斷是否開啟GPS定位
/** * 判斷是否有提供GPS定位 * @return */public boolean checkGPS() {
    boolean flag = false;

    if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)|| mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        flag = true;
    }

    return flag;
}
--------------
請求對方開啟GPS定位
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

沒有留言:

張貼留言