android获取gps坐标

38563424 贡献于2013-07-24

作者 微软系统  创建于2013-07-24 13:58:00   修改者微软系统  修改于2013-07-24 13:58:00字数2973

文档摘要:android获取gps坐标
关键词:

 private LocationManager mLocationManager; mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); updateMyLocation(mLocationManager); /**更新我的位置 * * */ String strLocationProvider="test"; public void updateMyLocation(LocationManager lm){ /* 第一次运行向Location Provider取得Location */ Location mLocation01 = getLocationProvider(lm); if(mLocation01!=null) { processLocationUpdated(mLocation01); } else { Toast.makeText(this,"位置初始化出错", Toast.LENGTH_SHORT).show(); } /* 创建LocationManager对象,监听Location更改时事件,更新MapView */ lm.requestLocationUpdates (strLocationProvider, 2000, 10, mLocationListener01); } public Location getLocationProvider(LocationManager lm) {//设定:地理位置获取的参数信息 Location retLocation = null; try { Criteria mCriteria01 = new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(false); mCriteria01.setBearingRequired(false); mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); strLocationProvider = lm.getBestProvider(mCriteria01, true); retLocation = lm.getLastKnownLocation(strLocationProvider); } catch(Exception e) { // mTextView01.setText(e.toString()); e.printStackTrace(); } return retLocation; } /**位置监听器*/ public final LocationListener mLocationListener01 = new LocationListener() { public void onLocationChanged(Location location) { // TODO Auto-generated method stub /* 当手机收到位置更改时,将location传入取得地理坐标 */ processLocationUpdated(location); } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub /* 当Provider已离开服务范围时 */ Toast.makeText(getApplicationContext(), "无地理位置信息可用", Toast.LENGTH_SHORT).show(); } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } public void onStatusChanged (String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }; /* 当手机收到位置更改,将location传入GeoPoint及MapView */ private void processLocationUpdated(Location location) { /* 传入Location对象,取得GeoPoint地理坐标 */ GeoPoint currentGeoPoint = getGeoByLocation(location); Log.i("service--------",currentGeoPoint.getLatitudeE6()+":"+currentGeoPoint.getLongitudeE6()); //读取记录的位置信息 Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVER2"); Bundle bundle = new Bundle(); bundle.putDouble("latitude", currentGeoPoint.getLatitudeE6()); bundle.putDouble("longitude", currentGeoPoint.getLongitudeE6()); intent.putExtra("location", bundle); // 发送广播 sendBroadcast(intent); Log.i("service--------", "been send"); Intent intent2 = new Intent("cn.yoyo.locationupdate"); Bundle bundle2 = new Bundle(); bundle2.putDouble("latitude", currentGeoPoint.getLatitudeE6()); bundle2.putDouble("longitude", currentGeoPoint.getLongitudeE6()); intent2.putExtra("location", bundle2); // 发送广播 sendBroadcast(intent2); Log.i("service--------", "been send"); // Toast.makeText(this, "情景已切换", Toast.LENGTH_SHORT).show(); } /**location->geopoint*/ public static GeoPoint getGeoByLocation(Location location) { GeoPoint gp = null; try { /* 当Location存在 */ if (location != null) { double geoLatitude = location.getLatitude()*1E6; double geoLongitude = location.getLongitude()*1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } catch(Exception e) { e.printStackTrace(); } return gp; }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档