Android编程实现GPS位置获取的方法
本文实例讲述了Android编程实现GPS位置获取的方法。分享给大家供大家参考,具体如下:
publicclassGPSInfoService{ privatestaticGPSInfoServicemInstance; privateLocationManagerlocationManager;//定位服务 privateGPSInfoService(Contextcontext){ //TODOAuto-generatedconstructorstub locationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE); } publicstaticGPSInfoServicegetInstance(Contextcontext){ if(mInstance==null){ mInstance=newGPSInfoService(context); } returnmInstance; } //注册定位监听 publicvoidregistenerLocationChangeListener(){ //得到所有的定位服务 //Listproviders=locationManager.getAllProviders(); //for(Stringprovider:providers){ //Log.i("i",provider); //} //查询条件 Criteriacriteria=newCriteria(); //定位的精准度 criteria.setAccuracy(Criteria.ACCURACY_FINE); //海拔信息是否关注 criteria.setAltitudeRequired(false); //对周围的事情是否进行关心 criteria.setBearingRequired(false); //是否支持收费的查询 criteria.setCostAllowed(true); //是否耗电 criteria.setPowerRequirement(Criteria.POWER_LOW); //对速度是否关注 criteria.setSpeedRequired(false); //得到最好的定位方式 Stringprovider=locationManager.getBestProvider(criteria,true); //注册监听 locationManager.requestLocationUpdates(provider,60000,0,getListener()); } //取消监听 publicvoidunRegisterLocationChangeListener(){ locationManager.removeUpdates(getListener()); } privateMyLocationListenerlistener; //得到定位的监听器 privateMyLocationListenergetListener(){ if(listener==null){ listener=newMyLocationListener(); } returnlistener; } //得到上个地理位置 publicStringgetLastLocation(){ returnsp.getString("last_location",""); } privatefinalclassMyLocationListenerimplementsLocationListener{ //位置的改变 publicvoidonLocationChanged(Locationlocation){ //TODOAuto-generatedmethodstub doublelatitude=location.getLatitude();//维度 doublelongitude=location.getLongitude();//经度 Stringlast_location="jingdu:"+longitude+",weidu:"+latitude; Editoreditor=sp.edit(); editor.putString("last_location",last_location); editor.commit(); } //gps卫星有一个没有找到 publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){ //TODOAuto-generatedmethodstub } //某个设置被打开 publicvoidonProviderEnabled(Stringprovider){ //TODOAuto-generatedmethodstub } //某个设置被关闭 publicvoidonProviderDisabled(Stringprovider){ //TODOAuto-generatedmethodstub } } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android视图View技巧总结》、《Android操作json格式数据技巧总结》、《Android开发入门与进阶教程》及《Android资源操作技巧汇总》
希望本文所述对大家Android程序设计有所帮助。