1、网络定位(network)。前提是必须连上网络:wifi、3G、2G;
获取到IP地址
例如:彩虹版QQ,珊瑚虫版QQ,就有一个功能显示对方的IP;
根据IP显示具体的位置;
原理是建立一个库那个IP地址对应那个地方;早期警方破案就采用此特点;
有局限性:针对固定的IP地址。
如果手机网或者ip地址是动态分布IP,这个偏差就很大。这种情况是无法满足需求的。
2、基站定位(passive)。
工作原理:手机能打电话,是需要基站的。手机定位也是用基站的。
手机附近能收到3个基站的信号,就可以定位了。
基站定位有可能很准确,比如基站多的地方;
如果基站少的话就会相差很大。
精确度:几十米到几公里不等;
3、GPS定位(gps)。
A-GPS 使用了卫星定位 需要联网辅助修正位置
特点是:需要搜索卫星, 头顶必须是空旷的;至少需要3颗卫星才能定位;不需要网络就能直接GPS定位
GPS直接定位,需要去寻找卫星的轨道之类的信息,需要花费一定时间,差不多1分钟,还很耗电
A-GPS则是优先访问网络,获取上次的卫星轨道信息,然后直接去定位
定位精度好的能到15米左右
影响条件:云层、大厦、大树。
卫星:美国人、欧洲人的卫星。
北斗:中国的,但没有民用,只是在大巴,战机等使用。
LocationManager 获取定位的管理者
根据中国国情,得到的坐标都是有偏差的,所以需要进行修改。
如果用于商业化,这个坐标需要花钱购买,测试用的话可以使用星火工具来修正坐标。
package com.itheima.gpsdemo;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
// 用到位置服务
private LocationManager lm;
private MyLocationListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
//得到三种定位提供者
// List<String> provider = lm.getAllProviders();
// for(String l: provider){
// System.out.println(l);
// }
listener = new MyLocationListener();
Criteria criteria = new Criteria();
//给位置提供者设置条件
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//设置参数细化:
//criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
//criteria.setAltitudeRequired(false);//不要求海拔信息
//criteria.setBearingRequired(false);//不要求方位信息
//criteria.setCostAllowed(true);//是否允许付费
//criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求
//得到最好的位置提供者 true表示获取的这个是可用的
String proveder= lm.getBestProvider(criteria, true);
//注册监听位置服务
//那种定位方式
//多久定位一次 gps的话60000一次,也就是一分钟一次,不要几秒一次,很耗电
//多少米定位一次 真机情况下 50米定位一次
lm.requestLocationUpdates(proveder, 0, 0, listener);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 取消监听位置服务
lm.removeUpdates(listener);
listener = null;
}
class MyLocationListener implements LocationListener {
/**
* 当位置改变的时候回调
*/
@Override
public void onLocationChanged(Location location) {
String longitude = "经度:" + location.getLongitude();
String latitude = "纬度:" + location.getLatitude();
String accuracy = "精确度:" + location.getAccuracy();
TextView textview = new TextView(MainActivity.this);
textview.setText(longitude + "\n" + latitude + "\n" + accuracy);
setContentView(textview);
}
/**
* 当状态发生改变的时候回调 开启--关闭 ;关闭--开启
*/
@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
}
}
}
package com.itheima.gpsdemo;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
// 用到位置服务
private LocationManager lm;
private MyLocationListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
//得到三种定位提供者
// List<String> provider = lm.getAllProviders();
// for(String l: provider){
// System.out.println(l);
// }
listener = new MyLocationListener();
Criteria criteria = new Criteria();
//给位置提供者设置条件
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//设置参数细化:
//criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
//criteria.setAltitudeRequired(false);//不要求海拔信息
//criteria.setBearingRequired(false);//不要求方位信息
//criteria.setCostAllowed(true);//是否允许付费
//criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求
//得到最好的位置提供者 true表示获取的这个是可用的
String proveder= lm.getBestProvider(criteria, true);
//注册监听位置服务
//那种定位方式
//多久定位一次 gps的话60000一次,也就是一分钟一次,不要几秒一次,很耗电
//多少米定位一次 真机情况下 50米定位一次
lm.requestLocationUpdates(proveder, 0, 0, listener);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 取消监听位置服务
lm.removeUpdates(listener);
listener = null;
}
class MyLocationListener implements LocationListener {
/**
* 当位置改变的时候回调
*/
@Override
public void onLocationChanged(Location location) {
String longitude = "经度:" + location.getLongitude();
String latitude = "纬度:" + location.getLatitude();
String accuracy = "精确度:" + location.getAccuracy();
TextView textview = new TextView(MainActivity.this);
textview.setText(longitude + "\n" + latitude + "\n" + accuracy);
setContentView(textview);
}
/**
* 当状态发生改变的时候回调 开启--关闭 ;关闭--开启
*/
@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
}
}
}
权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
火星地球坐标转化.地图坐标修偏
工具类
import java.io.InputStream;
import java.io.ObjectInputStream;
/**
* 火星地球坐标转化.地图坐标修偏
*
*/
public class ModifyOffset {
private static ModifyOffset modifyOffset;
static double[] X = new double[660 * 450];
static double[] Y = new double[660 * 450];
private ModifyOffset(InputStream inputStream) throws Exception {
init(inputStream);
}
public synchronized static ModifyOffset getInstance(InputStream is) throws Exception {
if (modifyOffset == null) {
modifyOffset = new ModifyOffset(is);
}
return modifyOffset;
}
public void init(InputStream inputStream) throws Exception {
ObjectInputStream in = new ObjectInputStream(inputStream);
try {
int i = 0;
while (in.available() > 0) {
if ((i & 1) == 1) {
Y[(i - 1) >> 1] = in.readInt() / 100000.0d;
;
} else {
X[i >> 1] = in.readInt() / 100000.0d;
;
}
i++;
}
} finally {
if (in != null)
in.close();
}
}
// standard -> china
public PointDouble s2c(PointDouble pt) {
int cnt = 10;
double x = pt.x, y = pt.y;
while (cnt-- > 0) {
if (x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d)
return pt;
int ix = (int) (10.0d * (x - 72.0d));
int iy = (int) (10.0d * (y - 10.0d));
double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
x = (x + pt.x + (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] + dx
* (1.0d - dy) * X[ix + 660 * iy + 1] + dx * dy
* X[ix + 660 * iy + 661] + (1.0d - dx) * dy
* X[ix + 660 * iy + 660] - x) / 2.0d;
y = (y + pt.y + (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] + dx
* (1.0d - dy) * Y[ix + 660 * iy + 1] + dx * dy
* Y[ix + 660 * iy + 661] + (1.0d - dx) * dy
* Y[ix + 660 * iy + 660] - y) / 2.0d;
}
return new PointDouble(x, y);
}
// china -> standard
public PointDouble c2s(PointDouble pt) {
int cnt = 10;
double x = pt.x, y = pt.y;
while (cnt-- > 0) {
if (x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d)
return pt;
int ix = (int) (10.0d * (x - 72.0d));
int iy = (int) (10.0d * (y - 10.0d));
double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
x = (x + pt.x - (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] - dx
* (1.0d - dy) * X[ix + 660 * iy + 1] - dx * dy
* X[ix + 660 * iy + 661] - (1.0d - dx) * dy
* X[ix + 660 * iy + 660] + x) / 2.0d;
y = (y + pt.y - (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] - dx
* (1.0d - dy) * Y[ix + 660 * iy + 1] - dx * dy
* Y[ix + 660 * iy + 661] - (1.0d - dx) * dy
* Y[ix + 660 * iy + 660] + y) / 2.0d;
}
return new PointDouble(x, y);
}
}
class PointDouble {
double x, y;
PointDouble(double x, double y) {
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ", y=" + y;
}
}
import java.io.InputStream;
import java.io.ObjectInputStream;
/**
* 火星地球坐标转化.地图坐标修偏
*
*/
public class ModifyOffset {
private static ModifyOffset modifyOffset;
static double[] X = new double[660 * 450];
static double[] Y = new double[660 * 450];
private ModifyOffset(InputStream inputStream) throws Exception {
init(inputStream);
}
public synchronized static ModifyOffset getInstance(InputStream is) throws Exception {
if (modifyOffset == null) {
modifyOffset = new ModifyOffset(is);
}
return modifyOffset;
}
public void init(InputStream inputStream) throws Exception {
ObjectInputStream in = new ObjectInputStream(inputStream);
try {
int i = 0;
while (in.available() > 0) {
if ((i & 1) == 1) {
Y[(i - 1) >> 1] = in.readInt() / 100000.0d;
;
} else {
X[i >> 1] = in.readInt() / 100000.0d;
;
}
i++;
}
} finally {
if (in != null)
in.close();
}
}
// standard -> china
public PointDouble s2c(PointDouble pt) {
int cnt = 10;
double x = pt.x, y = pt.y;
while (cnt-- > 0) {
if (x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d)
return pt;
int ix = (int) (10.0d * (x - 72.0d));
int iy = (int) (10.0d * (y - 10.0d));
double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
x = (x + pt.x + (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] + dx
* (1.0d - dy) * X[ix + 660 * iy + 1] + dx * dy
* X[ix + 660 * iy + 661] + (1.0d - dx) * dy
* X[ix + 660 * iy + 660] - x) / 2.0d;
y = (y + pt.y + (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] + dx
* (1.0d - dy) * Y[ix + 660 * iy + 1] + dx * dy
* Y[ix + 660 * iy + 661] + (1.0d - dx) * dy
* Y[ix + 660 * iy + 660] - y) / 2.0d;
}
return new PointDouble(x, y);
}
// china -> standard
public PointDouble c2s(PointDouble pt) {
int cnt = 10;
double x = pt.x, y = pt.y;
while (cnt-- > 0) {
if (x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d)
return pt;
int ix = (int) (10.0d * (x - 72.0d));
int iy = (int) (10.0d * (y - 10.0d));
double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
x = (x + pt.x - (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] - dx
* (1.0d - dy) * X[ix + 660 * iy + 1] - dx * dy
* X[ix + 660 * iy + 661] - (1.0d - dx) * dy
* X[ix + 660 * iy + 660] + x) / 2.0d;
y = (y + pt.y - (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] - dx
* (1.0d - dy) * Y[ix + 660 * iy + 1] - dx * dy
* Y[ix + 660 * iy + 661] - (1.0d - dx) * dy
* Y[ix + 660 * iy + 660] + y) / 2.0d;
}
return new PointDouble(x, y);
}
}
class PointDouble {
double x, y;
PointDouble(double x, double y) {
this.x = x;
this.y = y;
}
public String toString() {
return "x=" + x + ", y=" + y;
}
}
demo案例
public class Demo {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ModifyOffset offset = ModifyOffset.getInstance(Demo.class.getResourceAsStream("axisoffset.dat"));
PointDouble newdouble1 = offset.s2c(new PointDouble(116.29042787, 40.04337062));
System.out.println(newdouble1);
}
}
public class Demo {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ModifyOffset offset = ModifyOffset.getInstance(Demo.class.getResourceAsStream("axisoffset.dat"));
PointDouble newdouble1 = offset.s2c(new PointDouble(116.29042787, 40.04337062));
System.out.println(newdouble1);
}
}