android用户定位GPS开发(一)

 

一.User Location作用:1.获取用户的位置  , 2.追踪用户的移动

二.User Location关键API:
 1.LocationManager:用于管理android用户定位服务
 2.LocationProviders:提供多种定位方法供开发者选择    
 定位方法的分类:
  1.GPS定位 :使用GPS卫星进行定位,需要声明权限:

android.permission.ACCESS_FINE_LOCATION
         2.NETWORK定位 : 使用信号接收塔和wifi介入点进行定位,需要

声明权限:android.permission.ACCESS_FINE_LOCATION(比较精确的定位)或者

android.permission.ACCESS_COARSE_LOCATION(比较粗糙的定位)

三.获取用户当前位置:
 1.在清单文件中声明相应权限;
 2.获取LocationManager对象;
 3.选择LocationProvider;
 4.绑定LocationListener对象;

 

主activity:

public class LocationActivity extends Activity {
	private Button button ;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) this.findViewById(R.id.btnLocation);
        button.setOnClickListener(new ButtonListener());
    }
	
	private class ButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			//得到LocationManager对象
			LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			//绑定监听器
			//LocationManager.GPS_PROVIDER当前所使用的Location Provider
			//两个数字分别表示两次定位之间最小间隔时间和最小间隔距离
			//TestLocationListener监听器对象
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
		}
		
	}
	private class TestLocationListener implements LocationListener{

		@Override
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			System.out.println(location.getLongitude());//用户的经度
			System.out.println(location.getLatitude());//用户的纬度
			Toast.makeText(getApplicationContext(), location.getLongitude()+","+location.getLatitude(), 1).show();
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
	}
}


声明权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button 
    android:id="@+id/btnLocation"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="绑定监听器"/>
</LinearLayout>


用模拟器测试,从Emulate向模拟器发送经纬度数据,通过onLocationChanged()方法显示出来经纬度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值