一、 定位方式
现在的定位方式主要有以下三种:
1.纯硬件定位2.纯软件定位
3.软硬件混合定位方式
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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;
}
}