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

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值