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);
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。