一、用户定位涉及到得关键API:
location manager :管理用户的定位服务
location provider : 选择哪种途径获得用户位置
二、获取用户位置的途径:
GPS,需要在AndroidManifest.xml中声明:android.permission.ACCESS_FINE_LOCATION
NETWORK:信号塔或wifi,需要在androidMmanifest.xml中申明:android.permission.ACCESS_FINE_LOCATION
或 android.permisson.ACCESS_COARSE_LOCATION
三、实现的一般步骤:
1、在AndroidManifest.xml里定义权限
2、获取Location manager对象
3、选择location provider
4、绑定locationListener (位置移动时会触发某个时间)
四、代码参考:
AndroidManifest.xml
View Code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="max.userLocation" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".UserLocation" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
main.xml
View Code
<?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/buttonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定监听器" /> </LinearLayout>
userLocation.java
View Code
1 package max.userLocation; 2 3 import android.app.Activity; 4 import android.location.Location; 5 import android.location.LocationListener; 6 import android.location.LocationManager; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class UserLocation extends Activity { 13 /** Called when the activity is first created. */ 14 Button userLocationBtn = null; 15 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 userLocationBtn = (Button) findViewById(R.id.buttonId); 21 userLocationBtn.setOnClickListener(new userLocationBtnListener()); 22 } 23 class userLocationBtnListener implements OnClickListener { 24 25 @Override 26 public void onClick(View arg0) { 27 // TODO Auto-generated method stub 28 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 29 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new userLocationListener()); 30 } 31 32 } 33 class userLocationListener implements LocationListener { 34 35 @Override 36 public void onLocationChanged(Location arg0) { 37 // TODO Auto-generated method stub 38 System.out.println("经度:" + arg0.getLongitude()); 39 System.out.println("纬度:" + arg0.getLatitude()); 40 41 } 42 43 @Override 44 public void onProviderDisabled(String provider) { 45 // TODO Auto-generated method stub 46 47 } 48 49 @Override 50 public void onProviderEnabled(String provider) { 51 // TODO Auto-generated method stub 52 53 } 54 55 @Override 56 public void onStatusChanged(String provider, int status, Bundle extras) { 57 // TODO Auto-generated method stub 58 59 } 60 61 } 62 }