Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 地理服務

Android 地理服務

編輯:關於Android編程

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



    

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