18、GPS技术

GPS核心API

      Android SDK为GPS提供了很多API,其中LocationManager类是这些API的核心。LocationManager是一个系统服务类,与TelephonyManager、AudioManager等服务类的作用和创建服务类对象的方法类似。所有与GPS相关的操作都由LocationManager对象及其派生的对象完成。

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

     LocationProvider类可以获取与位置提供者相关的信息。

     Location类是对具体位置信息的抽象表示。

 

获取位置提供者

      使用GPS定位的关键之一就是获取LocationProvider,每一个LocationProvider对象都表示一个抽象的定位系统。无论使用GPS做什么,都需要首先获取合适的LocationProvider对象。

# 获取所有可用的LocationProvider(demo)。
 1 import java.util.List;
 2 import android.app.Activity;
 3 import android.content.Context;
 4 import android.location.LocationManager;
 5 import android.os.Bundle;
 6 import android.widget.ArrayAdapter;
 7 import android.widget.ListView;
 8 
 9 /**
10  * 得到 所有的LocationProvider。
11  * @author dr
12  *
13  */
14 public class AllProvidersActivity extends Activity {
15     ListView mProviders;
16     LocationManager mLocationManager;
17 
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22 
23         mProviders = (ListView) findViewById(R.id.providers);
24 
25         // 首先应获取LocationManager对象
26         mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
27 
28         // 获取所有的LocationProvder的名称
29         List<String> providerNames = mLocationManager.getAllProviders();
30 
31         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
32                 android.R.layout.simple_list_item_1, providerNames);
33 
34         mProviders.setAdapter(adapter);
35     }
36 }
 
# 根据条件(Criteria)获取LocationProvider(demo)。
 
 

根据名称获取LocationProvider

1 //  获取passive Location Provider
2 LocationProvider passiveProvider =
3 mLocationManager.getProvider(LocationManager.PASSIVE_PROVIDER);
4 //  获取gps Location Provider
5 LocationProvider gpsProvider =
6                mLocationManager.getProvider(LocationManager.GPS_PROVIDER);
7 //  获取network Location Provider
8 LocationProvider passiveProvider =
9                mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER);

 

实时获取GPS定位数据
18、GPS技术_第1张图片
 
 1 import android.app.Activity;
 2 import android.content.Context;
 3 import android.location.Location;
 4 import android.location.LocationListener;
 5 import android.location.LocationManager;
 6 import android.os.Bundle;
 7 import android.widget.EditText;
 8 
 9 public class LocationActivity extends Activity {
10     LocationManager mLocationManager;
11     EditText mEditText;
12 
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main);
17 
18         mEditText = (EditText) findViewById(R.id.show);
19 
20         mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
21 
22         // 从GPS获取最近的定位信息
23         Location location = mLocationManager
24                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);
25 
26         updateView(location);
27 
28         // 2000(2秒), 8(米)。
29         mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
30                 2000, 8, new LocationListener() {
31 
32                     @Override
33                     public void onStatusChanged(String provider, int status,
34                             Bundle extras) {
35                         // TODO Auto-generated method stub
36                     }
37 
38                     @Override
39                     public void onProviderEnabled(String provider) {
40                         // 当GPS Location Provider可用时,更新位置
41                         updateView(mLocationManager
42                                 .getLastKnownLocation(provider));
43                     }
44 
45                     @Override
46                     public void onProviderDisabled(String provider) {
47                         // TODO Auto-generated method stub
48                     }
49 
50                     @Override  // 位置发生变化。
51                     public void onLocationChanged(Location location) {
52                         // 当GPS定位信息发生改变时,更新位置
53                         updateView(location);
54                     }
55                 });
56 
57     }
58 
59     // 用来更新EditText中的信息
60     public void updateView(Location newLocation) {
61         if (newLocation != null) {
62             StringBuilder sb = new StringBuilder();
63             sb.append("实时的位置信息:\n");
64             sb.append("经度:");
65             sb.append(newLocation.getLongitude());
66             sb.append("\n纬度:");
67             sb.append(newLocation.getLatitude());
68             sb.append("\b高度:");
69             sb.append(newLocation.getAltitude());
70             sb.append("\n速度:");
71             sb.append(newLocation.getSpeed());
72             sb.append("\n方向:");
73             sb.append(newLocation.getBearing());
74             mEditText.setText(sb.toString());
75 
76         }
77     }
78 }
<!-- 授权获取定位信息 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 

测量地球上任意两点之间的距离
18、GPS技术_第2张图片
 
1 mEditText = (EditText) findViewById(R.id.edittext);
2         final float[] results = new float[3];
3 
4         Location.distanceBetween(20.123, 30.05644, 30.124, 40.543, results);
5         mEditText.setText(String.valueOf(results[0]) + "米");

 

邻近警告
18、GPS技术_第3张图片
 
 1 import mobile.android.proximity.alert.R;
 2 import android.app.Activity;
 3 import android.app.PendingIntent;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 import android.location.LocationManager;
 7 import android.os.Bundle;
 8 
 9 public class ProximityAlertActivity extends Activity {
10     @Override
11     public void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.main);
14 
15         LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
16 
17         double longitude = 123.427109;
18         double latitude = 41.764998;
19         float radius = 2000;  //距离中心点半径,单位:米。
20 
21         Intent intent = new Intent(this, PromimityAlertRecivery.class);
22 
23         // 任何情况下都可以发intent,除非关机。intent的话如果关闭当前activity或者程序,将终止。
24         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,
25                 intent, 0);
26         
27         locationManager.addProximityAlert(latitude, longitude, radius, -1,
28                 pendingIntent);
29     }
30 }
 1 import android.content.Context;
 2 import android.content.Intent;
 3 import android.location.LocationManager;
 4 import android.widget.Toast;
 5 
 6 /**
 7  * 广播接收器
 8  * @author dr
 9  */
10 public class PromimityAlertRecivery {
11     public void onReceive(Context context, Intent intent) {
12         // 获取是否进入指定区域
13         boolean isEnter = intent.getBooleanExtra(
14                 LocationManager.KEY_PROXIMITY_ENTERING, false);
15         if (isEnter) {
16             Toast.makeText(context, "您已经进入指定区域", Toast.LENGTH_LONG).show();
17         } else {
18             Toast.makeText(context, "您已经离开指定区域", Toast.LENGTH_LONG).show();
19         }
20     }
21 }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="mobile.android.proximity.alert"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="cn.proximity.alert.ProximityAlertActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="cn.proximity.alert.ProximityAlertReciever" />
    </application>
    <!-- 授权获取定位信息 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>

</manifest> 

 

 
 
 
 
 
 
 
 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(gps)