标签:
我去,好久对没来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,亲测三星是可以的,比较仓促,不足之处留言。
标签:
原文地址:http://my.oschina.net/idip/blog/477076