Android基於位置的服務

一、 定位方式

現在的定位方式主要有以下三種:

1.純硬件定位
  需要GPS硬件支持,直接和衛星交互來獲取當前經緯度

2.純軟件定位
  一種是通過WIFI連接來確認熱點的位置 然後給出一個比較大概的位置(獲得WIFI的AP地址之後是需要連接WIFI數據庫來獲得真正的地址的 )
  一種是通過移動基站的MSC(Mobile Switching Center移動通信系統)交互來確認你註冊的是哪個基站 以及基站的位置(可能和多個基站交互來獲取較精確的位置信息)

3.軟硬件混合定位方式

  AGPS 先通過軟件來獲取大概位置 然後得到此區域的衛星序列 和衛星通信

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

There are 3 location providers in Android (ranging from 1.5 to 2.2). They are:

  • gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location usingsatellites. Depending on conditions, this provider may take a while to return a locationfix. Requires the permission android.permission.ACCESS_FINE_LOCATION.
  • network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. This provider determines location based on availability ofcell tower andWiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.
  • passive –> (CellID, WiFi MACID): A special location provider for receiving locations without actually initiating a location fix. This provider can be used topassively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider willreturn locations generated by other providers. Requires the permission android.permission.ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only returncoarse fixes.

This is what Android calls these location providers, however, the underlying technologies to make this stuff work is mapped to the specific set of hardware and telco provided capabilities (network service).

二、定位代碼

         1) AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.orange.lbstest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />

       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />      <!--GPS和WiFi  -->
 <!-- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> -->      <!--WiFi  -->
         <uses-permission android:name="android.permission.INTERNET" />                                                  
 <!-- <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> --><!--LocationManager.sendExtraCommand need this permission exactly .拓展Location Manager API ,冷啓動,溫啓動,熱啓動 -->
 <!-- <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />-->   <!--允許應用創建用於測試的模擬Location Provider  -->

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.orange.lbstest.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>


         2 )MainActivity.java文件

package com.orange.lbstest;

import java.util.List;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

/**
 * 
 * @author Orange
 * @date 1/28/2014
 */
public class MainActivity extends Activity {
    LocationManager mLocationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            /**
             * 獲取位置服務
             */
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            /**
             * 獲取所有的Location Provider,並在Logcat中輸出
             */
            List<String> providerLists = mLocationManager.getAllProviders();
            for (String p : providerLists) {
                Log.v("providers", p);
            }
            /**
             * 分別輸出不同Location Provider是否可用
             */
            Log.i("isGPSEnabled",
                    mLocationManager
                            .isProviderEnabled(LocationManager.GPS_PROVIDER)
                            + "");
            Log.i("isWiFiEnabled",
                    mLocationManager
                            .isProviderEnabled(LocationManager.NETWORK_PROVIDER)
                            + "");
            Log.i("isPassiveEnabled",
                    mLocationManager
                            .isProviderEnabled(LocationManager.PASSIVE_PROVIDER)
                            + "");
            /**
             * 分別獲取GPS座標和WiFi座標
             */
            Location mGPSLocation = mLocationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);// GPS
            Location mWiFiLocation = mLocationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);// NetWork

            Criteria criteria = new Criteria();// 根據當前設備情況自動選擇Location Provider
            criteria.setAccuracy(Criteria.ACCURACY_FINE);// 最大精度
            criteria.setAltitudeRequired(false);// 不要求海拔信息
            criteria.setBearingRequired(false);// 不要求方位精度
            criteria.setCostAllowed(true);// 允許付費
            criteria.setPowerRequirement(Criteria.POWER_LOW);// 對電量的要求
            Location mCriteriaLocation = mLocationManager
                    .getLastKnownLocation(mLocationManager.getBestProvider(
                            criteria, true));

            Log.w("GPSLocation:", mGPSLocation.getLongitude() + ","
                    + mGPSLocation.getLatitude());
            Log.w("WiFiLocation:", mWiFiLocation.getLongitude() + ","
                    + mWiFiLocation.getLatitude());
            Log.w("AutoProviderLocation:", mCriteriaLocation.getLongitude()
                    + "," + mCriteriaLocation.getLatitude());

            /**
             * 註冊位置更新事件(參數中也可用NETWORK_PROVIDER)
             */
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 3000, 0, mLocationListener);
        } catch (Exception e) {
            // TODO: handle exception
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        }
    }

    LocationListener mLocationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            Log.v("GPSLocation",
                    location.getLongitude() + "," + location.getLatitude());
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        /**
         * 取消位置更新事件(參數中也可用NETWORK_PROVIDER)
         */
        mLocationManager.removeUpdates(mLocationListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}




發佈了45 篇原創文章 · 獲贊 16 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章