android132 360 05 手机定位

时间:2023-11-30 22:11:56
.网络定位:根据ip地址定位,根据ip地址在实际地址数据库中查询实际地址。
缺点:动态ip导致地址不准确。
.基站定位:3个基站就可以确定实际位置,定位范围是几百米到几公里不等。
.GPS定位:美国卫星定位系统(北斗定位是中国的),至少3颗卫星就可以定位,GPS使用24颗卫星基本就可以覆盖全球90%范围,GPS不需要网络只要能够接受卫星信号就可以了,定位的精度比较准确几米到几十米不等。缺点是容易受云层和建筑干扰。
.安卓采用A-GPS(辅助GPS定位系统),弥补GPS定位缺点,可以通过网络和GPS共同定位,如果GPS信号不强就用网络,网络信号不强就用GPS.范围是几米到几十米。
package com.itheima52.location;

import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
/*
<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"/> 模拟器模拟的经纬度坐标,
*/ //获取经纬度
public class MainActivity extends Activity { private TextView tvLocation;
private LocationManager lm;
private MyLocationListener listener; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLocation = (TextView) findViewById(R.id.tv_location);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
List<String> allProviders = lm.getAllProviders();// 获取所有位置提供者(网络(NETWORK_PROVIDER = "network"),基站(PASSIVE_PROVIDER = "passive"),GPS(GPS_PROVIDER = "gps"))
System.out.println(allProviders);
listener = new MyLocationListener();
// 参1表示位置提供者(通过GPS更新),参2表示最短更新位置时间0表示立即更新,参3表示最短更新位置距离0表示立即更新。
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
} class MyLocationListener implements LocationListener { // 位置发生变化回调
@Override
public void onLocationChanged(Location location) {
String j = "经度:" + location.getLongitude();
String w = "纬度:" + location.getLatitude();
String accuracy = "精确度:" + location.getAccuracy();
String altitude = "海拔:" + location.getAltitude();
tvLocation.setText(j + "\n" + w + "\n" + accuracy + "\n" + altitude);
} // 位置提供者状态发生变化(GPS没有信号变成有信号)
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("onStatusChanged");
} // 用户打开gps
@Override
public void onProviderEnabled(String provider) {
System.out.println("onProviderEnabled");
} // 用户关闭gps
@Override
public void onProviderDisabled(String provider) {
System.out.println("onProviderDisabled");
} } @Override
protected void onDestroy() {
super.onDestroy();
lm.removeUpdates(listener);// 手机定位其实是启动了一个服务,当activity销毁时,服务不会停止,所以要手动停止更新位置, 节省电量,
} }
public class Demo {
//火星坐标
public static void main(String[] args) {
try {
ModifyOffset instance = ModifyOffset.getInstance(ModifyOffset.class
.getResourceAsStream("axisoffset.dat"));
PointDouble s2c = instance.s2c(new PointDouble(116.29049474,
40.04302021));// 标准坐标转化为火星坐标 System.out.println(s2c);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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;
}
}

通过service启动坐标定位:

package com.itheima52.mobilesafe.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder; /**
* 获取经纬度坐标的service
* 清单文件注册:<service android:name=".service.LocationService" ></service>
*
* //Context context;
context.startService(new Intent(context, LocationService.class));// 开启定位服务,调用LocationService的onCreate方法。
SharedPreferences sp = context.getSharedPreferences("config",Context.MODE_PRIVATE);
String location = sp.getString("location","getting location...");
System.out.println("location:" + location);
abortBroadcast();// 中断短信的传递, 从而系统短信app就收不到内容了,不中断则系统短信还是能够接收到短信。
*/
public class LocationService extends Service { private LocationManager lm;
private MyLocationListener listener;
private SharedPreferences mPref; @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
super.onCreate();
mPref = getSharedPreferences("config", MODE_PRIVATE);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
// List<String> allProviders = lm.getAllProviders();// 获取所有位置提供者
// System.out.println(allProviders);
Criteria criteria = new Criteria();
criteria.setCostAllowed(true);// 是否允许付费,比如使用3g网络定位,如果设为false则没有GPS只有流量因为要流量钱就不开启。
criteria.setAccuracy(Criteria.ACCURACY_FINE);//精确度为fine:一般的精确度。
String bestProvider = lm.getBestProvider(criteria, true);// 获取最佳位置提供者,true表示Provider可用就返回,false表示不管可不可用都返回。
listener = new MyLocationListener();
lm.requestLocationUpdates(bestProvider, 0, 0, listener);// 参1表示位置提供者,参2表示最短更新时间,参3表示最短更新距离
} //
class MyLocationListener implements LocationListener { // 位置发生变化
@Override
public void onLocationChanged(Location location) {
System.out.println("get location!");
// 将获取的经纬度保存在sp中
mPref.edit()
.putString(
"location",
"j:" + location.getLongitude() + "; w:"
+ location.getLatitude()).commit(); stopSelf();//停掉service
}
// 位置提供者状态发生变化
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("onStatusChanged");
}
// 用户打开gps
@Override
public void onProviderEnabled(String provider) {
System.out.println("onProviderEnabled");
}
// 用户关闭gps
@Override
public void onProviderDisabled(String provider) {
System.out.println("onProviderDisabled");
}
} @Override
public void onDestroy() {
super.onDestroy();
lm.removeUpdates(listener);// 当activity销毁时,停止更新位置, 节省电量,否则服务会一直开启。
}
}