日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 高德地图sdk连续定位,高德地图实战:后台持续定位实现

發布時間:2024/1/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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());

//設置定位回調監聽

mLocationClient.setLocationListener(mLocationListener);

//初始化AMapLocationClientOption對象

mLocationOption = new AMapLocationClientOption();

//設置定位模式為AMapLocationMode.Hight_Accuracy,高精度模式。

mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);

//獲取一次定位結果:

//該方法默認為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;

/**

* 收發網絡數據的線程

*/

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: //發送位置

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(){

//給定位客戶端對象設置定位參數

mLocationClient.setLocationOption(mLocationOption);

//啟動定位

mLocationClient.startLocation();

}

//聲明定位回調監聽器

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();//獲取經度

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/";

// 調用的方法名稱

String methodName = "UpdateDevicePosition";

// EndPoint

String endPoint = "http://182.247.238.98:82/WebService1.asmx";

// SOAP Action

String soapAction = "http://nfswit.cc/UpdateDevicePosition";

// 指定WebService的命名空間和調用的方法名

SoapObject rpc = new SoapObject(nameSpace, methodName);

// 指定參數

rpc.addProperty("strDeviceMAC", mac);

rpc.addProperty("strDevicePosition", position);

rpc.addProperty("strDeviceRecordTime", recordTime);

rpc.addProperty("strDeviceReportTime", reportTime);

// 生成調用WebService方法的SOAP請求信息,并指定SOAP的版本

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.implicitTypes = true;

envelope.bodyOut = rpc;

// 設置是否調用的是dotNet開發的WebService

envelope.dotNet = true;

// 等價于envelope.bodyOut = rpc;

envelope.setOutputSoapObject(rpc);

HttpTransportSE transport = new HttpTransportSE(endPoint);

try {

// 調用WebService

transport.call(soapAction, envelope);

} catch (Exception e) {

e.printStackTrace();

}

// 獲取返回的數據

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

// 獲取返回的結果

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

}

}

}

}

總結

以上是生活随笔為你收集整理的android 高德地图sdk连续定位,高德地图实战:后台持续定位实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。