1.User Location 能做什么
获取用户位置
追踪用户的移动
2.User Location 的关键API
Location Manager:用于管理Android的用户定位服务;
Location Providers: 提供多种定位方式供开发者选择;
3.获取用户的当前位置
Gps定位:
声明权限:android.permission.ACCESS_FINE_LOCATION
network定位:
声明权限:android.permission.ACCESS_FINE_LOCATION(精确)
或 android.permission.ACCESS_COARSE_LOCATION(不精确)
步骤:
1.在AndroidManifest.xml中声明相应的权限
2.获取LocationManager对象
3.选择LocationProvider
4.绑定LocationListener对象
4.获取最佳的LocationProvider
criteria可以设置一系列的查询条件,用于查找当前设备当中符合条件的LocationProvider
查询条件:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
criteria.setAltitudeRequired(true);//要求海拔信息
criteria.setBearingRequired(true);//要求方位信息
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);//要求方位信息 的精确度
criteria.setCostAllowed(false);//是否允许付费
criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求
criteria.setSpeedAccuracy(criteria.ACCURACY_HIGH);//对速度的精确度
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);//对水平的精确度
criteria.setSpeedRequired(true);//要求速度信息
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);//对垂直精度
String providerFalse = locationManager.getBestProvider(criteria, false);//找到最好的Provider不管是否能用。
String providerTrue = locationManager.getBestProvider(criteria, true);//找到最好的能用的Provider。
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/buttonProvider" android:text="测试当前设备的provider" /> <Button android:layout_below="@id/buttonProvider" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/buttonProviderBest" android:text="最好的provider" /> <TextView android:id="@+id/AltitudeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/buttonProviderBest" android:layout_below="@+id/buttonProviderBest" android:text="海拔/m" android:textSize="20dp" /> <TextView android:id="@+id/BearingText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/AltitudeText" android:layout_below="@+id/AltitudeText" android:layout_marginTop="15dp" android:text="方位" android:textSize="20dp" /> <TextView android:id="@+id/AltitudeValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/BearingText" android:layout_alignRight="@+id/buttonProvider" android:text="海拔" android:textSize="20dp" /> <TextView android:id="@+id/BearingValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/BearingText" android:layout_alignBottom="@+id/BearingText" android:layout_alignLeft="@+id/AltitudeValue" android:text="方位" android:textSize="20dp" /> <TextView android:id="@+id/SpeedValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/SpeedText" android:layout_alignBottom="@+id/SpeedText" android:layout_alignLeft="@+id/BearingValue" android:text="速度 " android:textSize="20dp" /> <TextView android:id="@+id/SpeedText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/BearingText" android:layout_below="@+id/BearingText" android:layout_marginTop="21dp" android:text="速度 s/m" android:textSize="20dp" /> <TextView android:id="@+id/LongitudeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/SpeedText" android:layout_below="@+id/SpeedText" android:layout_marginTop="21dp" android:text="经度t" android:textSize="20dp" /> <TextView android:id="@+id/LongitudeValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/LongitudeText" android:layout_alignBottom="@+id/LongitudeText" android:layout_alignLeft="@+id/SpeedValue" android:text="经度" android:textSize="20dp" /> <TextView android:id="@+id/LatitudeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/SpeedText" android:layout_below="@+id/LongitudeValue" android:layout_marginTop="19dp" android:text="纬度t" android:textSize="20dp" /> <TextView android:id="@+id/LatitudeValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/LatitudeText" android:layout_alignBottom="@+id/LatitudeText" android:layout_alignLeft="@+id/SpeedValue" android:text="纬度" android:textSize="20dp" /> </RelativeLayout>
activity.java
package com.yuexin.location01; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button buttonProvider = null; private Button buttonProviderBest = null; private LocationManager locationManager ; private TextView AltitudeValue= null; private TextView BearingValue= null; private TextView SpeedValue= null; private TextView LongitudeValue= null; private TextView LatitudeValue= null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AltitudeValue = (TextView) findViewById(R.id.AltitudeValue); BearingValue = (TextView) findViewById(R.id.BearingValue); SpeedValue = (TextView) findViewById(R.id.SpeedValue); LongitudeValue = (TextView) findViewById(R.id.LongitudeValue); LatitudeValue = (TextView) findViewById(R.id.LatitudeValue); buttonProvider = (Button) findViewById(R.id.buttonProvider); buttonProviderBest = (Button) findViewById(R.id.buttonProviderBest); buttonProvider.setOnClickListener(new ButtonListener()); buttonProviderBest.setOnClickListener(new ButtonListrnerBest()); //得到LocationManager对象 locationManager = (LocationManager) MainActivity.this.getSystemService(LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()); } class ButtonListrnerBest implements OnClickListener{ @SuppressLint("NewApi") @Override public void onClick(View arg0) { Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度 criteria.setAltitudeRequired(true);//要求海拔信息 criteria.setBearingRequired(true);//要求方位信息 criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);//要求方位信息 的精确度 criteria.setCostAllowed(false);//是否允许付费 criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求 criteria.setSpeedAccuracy(criteria.ACCURACY_HIGH);//对速度的精确度 criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);//对水平的精确度 criteria.setSpeedRequired(true);//要求速度信息 criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);//对垂直精度 String providerFalse = locationManager.getBestProvider(criteria, false);//找到最好的Provider不管是否能用。 String providerTrue = locationManager.getBestProvider(criteria, true);//找到最好的能用的Provider。 //locationManager.requestLocationUpdates(providerTrue, 0, 0, new LocationListener()); System.out.println("providerFalse---->"+providerFalse); System.out.println("providerTrue---->"+providerTrue); } } class ButtonListener implements OnClickListener{ @Override public void onClick(View arg0) { /* //得到LocationManager对象 LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(LOCATION_SERVICE); // 1.定义当前使用的LocationManager // 2.定义两次定位的间隔大小。 // 3.两次定位之间的最小距离 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()); */ List<String> allProvider = locationManager.getAllProviders(); for (int i = 0; i < allProvider.size(); i++) { System.out.println(allProvider.get(i)); } } } private class LocationListener implements android.location.LocationListener{ @Override public void onLocationChanged(Location location) { // 设备位置发生改变Location为对象的位置 System.out.println(location.getLongitude());//经度 System.out.println(location.getLatitude());//维度 AltitudeValue.setText(String.format("%.8f",location.getAltitude())); BearingValue.setText(String.format("%.8f",location.getBearing())); SpeedValue.setText(String.format("%.8f",location.getSpeed())); LongitudeValue.setText(String.format("%.8f",location.getLongitude())); LatitudeValue.setText(String.format("%.8f",location.getLatitude())); } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yuexin.location01" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yuexin.location01.MainActivity" 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>
Android 地理服务