Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 地理定位服務完全指南

編輯:關於 Android 程式設計

1. User Location 能為你做什麼

獲取使用者當前位置。

追蹤使用者的移動軌跡。

2. User Location 的關鍵 API

LocationManager:負責管理 Android 的使用者定位服務。

LocationProvider:提供多種定位方式,供開發者依需求挑選。

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

 

MainActivity.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 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

           

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved