您的位置:首页 > 移动开发 > Android开发

Android定位获得经纬度(手机位置服务和百度定位)

2014-12-23 16:28 477 查看
private double latitude = 0.0;
	private double longitude = 0.0;
<span style="white-space:pre">	</span>//利用手机位置服务定位
	void getLoaction() {
		final LocationManager locationManager = (LocationManager) getActivity()
				.getSystemService(Context.LOCATION_SERVICE);
		
		//创建一个criteria对象  
        Criteria criteria = new Criteria();     
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);  
        //设置不需要获取海拔方向数据  
        criteria.setAltitudeRequired(false);     
        criteria.setBearingRequired(false);  
        //设置允许产生资费  
        criteria.setCostAllowed(true);     
        //要求低耗电  
        criteria.setPowerRequirement(Criteria.POWER_LOW);     
        String provider = locationManager.getBestProvider(criteria, false);     
        Log.i(TAG, "we choose "+ provider);  
        Location location = locationManager.getLastKnownLocation(provider);  
        //第一次获得设备的位置  
        updateLocation(location); 
        //重要函数,监听数据测试  
        locationManager.requestLocationUpdates(provider, 6000, 10,     
                       locationListener);     
		
	}
	
	 //创建一个事件监听器  
	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			updateLocation(location);
		}

		public void onProviderDisabled(String provider) {
			updateLocation(null);
			Log.i(TAG, "Provider now is disabled..");
		}

		public void onProviderEnabled(String provider) {
			Log.i(TAG, "Provider now is enabled..");
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};   
	      
	//获取用户位置的函数,利用Log显示  
	private void updateLocation(Location location) {
		Log.i(TAG, "updateLocation");
		if (location != null) {
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			
			String oldLocation = MainUIActivity.mApplication.getLocation();
			String newLocation = longitude + "," + latitude;
			mCurCommunityPoint.setText(newLocation);
			newLocation = Utils.bd_encrypt(latitude, longitude);
			Log.i(TAG, "The location has changed.. " + newLocation);
			if (!newLocation.equals(oldLocation)) {
				mCurPageIndex = 1;
				mHandler.obtainMessage(GET_COMMUNITY_DATA,
						newLocation).sendToTarget();
			}
			
		}
	}
	//百度位置服务,百度定位sdk下载地址:http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0
	void getBaiduLoaction(){
		LocationClient mLocationClient = new LocationClient(getActivity());
		mLocationClient.registerLocationListener( new BDLocationListener() {
			
			@Override
			public void onReceiveLocation(BDLocation location) {
				onLocationChanged(location);
			}
		} );
		
		//注册监听函数
	    
	    LocationClientOption option = new LocationClientOption();
	    option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
	    option.setCoorType("gcj02");//返回的定位结果是百度经纬度,默认值gcj02
	    option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
	    option.setIsNeedAddress(true);//返回的定位结果包含地址信息
	    option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
	    mLocationClient.setLocOption(option);
	    
	    mLocationClient.start();
	}
	
	/**
	 * 当位置发生变化时触发此方法
	 * 
	 * @param location 当前位置
	 */
	public void onLocationChanged(BDLocation location) {
		if (location != null) {
			// 显示定位结果
			Log.d(TAG, location.getLongitude() + "  " + location.getLatitude());
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			
			String oldLocation = MainUIActivity.mApplication.getLocation();
			String newLocation = longitude + "," + latitude;
			mCurCommunityPoint.setText(newLocation);
			newLocation = Utils.bd_encrypt(latitude, longitude);
			Log.i(TAG, "The location has changed.. " + newLocation);
			if (!newLocation.equals(oldLocation)) {
				mCurPageIndex = 1;
				mHandler.obtainMessage(GET_COMMUNITY_DATA,
						newLocation).sendToTarget();
			}
		}
	}


从准确度来说百度还是比较靠谱的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐