android 高德地图sdk连续定位,高德地图实战:后台持续定位实现
public class LocationService extends Service {
private static final String TAG = "LocationService";
//聲明AMapLocationClient類對象
AMapLocationClient mLocationClient = null;
//聲明AMapLocationClientOption對象
public AMapLocationClientOption mLocationOption = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "start LocationService!");
netThread.start();
//初始化定位
mLocationClient = new AMapLocationClient(getApplicationContext());
//設置定位回調(diào)監(jiān)聽
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption對象
mLocationOption = new AMapLocationClientOption();
//設置定位模式為AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
//獲取一次定位結(jié)果:
//該方法默認為false。
mLocationOption.setOnceLocation(true);
mLocationOption.setOnceLocationLatest(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "StartCommand LocationService!");
getPosition();
return super.onStartCommand(intent, flags, startId);
}
Handler netHandler = null;
/**
* 收發(fā)網(wǎng)絡數(shù)據(jù)的線程
*/
Thread netThread = new Thread(){
@Override
public void run() {
Looper.prepare();
netHandler = new Handler(){
public void dispatchMessage(Message msg) {
Bundle data = msg.getData();
switch(msg.what){
case 0x1: //發(fā)送位置
String macstr = getMac();
String longitude = data.getString("longitude");
String latitude = data.getString("latitude");
String timestr = data.getString("timestr");
upDatePosition(macstr,longitude+","+latitude,timestr,timestr);
break;
}
};
};
Looper.loop();
}
};
public void getPosition(){
//給定位客戶端對象設置定位參數(shù)
mLocationClient.setLocationOption(mLocationOption);
//啟動定位
mLocationClient.startLocation();
}
//聲明定位回調(diào)監(jiān)聽器
public AMapLocationListener mLocationListener = new AMapLocationListener(){
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if(amapLocation==null){
Log.i(TAG, "amapLocation is null!");
return;
}
if(amapLocation.getErrorCode()!=0){
Log.i(TAG, "amapLocation has exception errorCode:"+amapLocation.getErrorCode());
return;
}
Double longitude = amapLocation.getLongitude();//獲取經(jīng)度
Double latitude = amapLocation.getLatitude();//獲取緯度
String longitudestr = String.valueOf(longitude);
String latitudestr = String.valueOf(latitude);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
String timestr = df.format(date);
Log.i(TAG, "longitude,latitude:"+longitude+","+latitude);
Log.i(TAG, "time:"+timestr);
Message msg = new Message();
Bundle data = new Bundle();
data.putString("longitude", longitudestr);
data.putString("latitude", latitudestr);
data.putString("timestr", timestr);
msg.setData(data);
msg.what = 0x1;
netHandler.sendMessage(msg);
}
};
private String getMac() {
String macSerial = null;
String str = "";
try {
Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str;) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 賦予默認值
ex.printStackTrace();
}
return macSerial;
}
public void upDatePosition(String mac, String position, String recordTime, String reportTime) {
Log.i(TAG, mac+";"+position+";"+recordTime);
// 命名空間
String nameSpace = "http://nfswit.cc/";
// 調(diào)用的方法名稱
String methodName = "UpdateDevicePosition";
// EndPoint
String endPoint = "http://182.247.238.98:82/WebService1.asmx";
// SOAP Action
String soapAction = "http://nfswit.cc/UpdateDevicePosition";
// 指定WebService的命名空間和調(diào)用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 指定參數(shù)
rpc.addProperty("strDeviceMAC", mac);
rpc.addProperty("strDevicePosition", position);
rpc.addProperty("strDeviceRecordTime", recordTime);
rpc.addProperty("strDeviceReportTime", reportTime);
// 生成調(diào)用WebService方法的SOAP請求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.bodyOut = rpc;
// 設置是否調(diào)用的是dotNet開發(fā)的WebService
envelope.dotNet = true;
// 等價于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 調(diào)用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 獲取返回的數(shù)據(jù)
Object object = envelope.bodyIn;
// SoapFault object = (SoapFault) envelope.bodyIn;
if (object == null) {
Log.i(TAG, "return object is null!");
return;
}
if (object instanceof SoapFault) {
Log.i(TAG, "SoapFault refult is :" + object.toString());
return;
} else if (object instanceof SoapObject) {
// 獲取返回的結(jié)果
Log.i(TAG, "SoapObject refult is :" + object.toString());
try {
SoapObject result = (SoapObject) object;
PropertyInfo info = new PropertyInfo();
result.getPropertyInfo(0, info);
String str = result.getProperty(0).toString();
Log.i(TAG, str);
JSONObject jsonobj = (JSONObject) JSONValue.parse(str);
String code = (String) jsonobj.get("code");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
總結(jié)
以上是生活随笔為你收集整理的android 高德地图sdk连续定位,高德地图实战:后台持续定位实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 被动抓病毒的日子(2)【入侵大佬:xia
- 下一篇: AMBA3.0协议——AXI(Advan