您的位置:首页 > 移动开发 > Android开发

Android_获取手机位置

2014-09-09 22:14 387 查看
PS:看了9年的小说,自己开始动手写了一本,请各位猿们动动手指,点击下,有起点账号的可以收藏下!!《武意长存》

获取手机位置的方式

1) 网络定位(network)。前提是必须连上网络:wifi、3G、2G;

获取到IP地址

例如:传美版QQ,彩虹版QQ,珊瑚虫版QQ,就有一个功能显示对方的IP;

根据IP显示具体的位置;

原理是建立一个库那个IP地址对应那个地方;早期警方破案就采用此特点;

有局限性:针对固定的IP地址。

如果手机网或者ip地址是动态分布IP,这个偏差就很大。这种情况是无法满足需求的。

2) 基站定位(passive)。

工作原理:手机能打电话,是需要基站的。手机定位也是用基站的。

手机附近能收到3个基站的信号,就可以定位了。

基站定位有可能很准确,比如基站多的地方;

如果基站少的话就会相差很大。

精确度:几十米到几公里不等;

3) GPS定位(gps)。

A-GPS 使用了卫星定位 需要联网辅助修正位置

需要3颗卫星;

特点是:需要搜索卫星, 头顶必须是空旷的;

影响条件:云层、大厦、大树。

卫星:美国人、欧洲人的卫星。

北斗:中国的,但没有民用,只是在大巴,战机等使用。

精确度:15米左右

在manifest文件中添加权限

<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"/>
<uses-permission android:name="android.permission.INTERNET"/>


具体代码如下

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

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);		//设置为最大精度
criteria.setAltitudeRequired(false);				//不要求海拔信息
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//对电量的要求
criteria.setCostAllowed(true); 		//允许产生费用
criteria.setSpeedRequired(true);

listener = new MyLocationListener();
// 第二个参数:两次位置更新的时间间隔
lm.requestLocationUpdates(lm.getBestProvider(criteria, true), 0, 0, listener);

}

class MyLocationListener implements LocationListener {
// 当位置发生变化 执行者方法
@Override
public void onLocationChanged(Location location) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
double accuracy = location.getAccuracy();

// 把标准的GPS坐标转换成火星坐标
// 由于国内的坐标是进过加偏处理,所以我们需要把标准的坐标转成国内的坐标
InputStream is;
try {
is = getAssets().open("axisoffset.dat");
ModifyOffset offset = ModifyOffset.getInstance(is);
PointDouble point = offset.s2c(new PointDouble(longitude, latitude));

StringBuffer sb = new StringBuffer();
sb.append("经度:").append(point.x).append("\n")
.append("纬度:").append(point.y).append("\n")
.append("精度:").append(accuracy).append("\n");
TextView textView = new TextView(getApplicationContext());
textView.setText(sb.toString());

setContentView(textView);
} catch (Exception e) {
e.printStackTrace();
}
}

// 当某一个位置提供者状态发生变化的时候 关闭--》开启 或者开启--》关闭
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

}

@Override
protected void onDestroy() {
super.onDestroy();
if(lm!=null && listener!=null){
lm.removeUpdates(listener);
listener = null;
}
}
}
</pre><pre name="code" class="java"><pre name="code" class="java">/**
* 火星地球坐标转化.地图坐标修偏
*
*/
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;
}
}






axisoffset.dat文件下载 :http://yunpan.cn/Q7qQLraKKrPae 提取码 5957
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: