本文实例为大家分享了Android实现地图轨迹的具体代码,供大家参考,具体内容如下
mainActivity.java
package com.example.amapdemo; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; import com.amap.api.track.AMapTrackClient; import com.amap.api.track.ErrorCode; import com.amap.api.track.OnTrackLifecycleListener; import com.amap.api.track.TrackParam; import com.amap.api.track.query.entity.LocationMode; import com.amap.api.track.query.model.AddTerminalRequest; import com.amap.api.track.query.model.AddTerminalResponse; import com.amap.api.track.query.model.AddTrackResponse; import com.amap.api.track.query.model.DistanceResponse; import com.amap.api.track.query.model.HistoryTrackResponse; import com.amap.api.track.query.model.LatestPointResponse; import com.amap.api.track.query.model.OnTrackListener; import com.amap.api.track.query.model.ParamErrorResponse; import com.amap.api.track.query.model.QueryTerminalRequest; import com.amap.api.track.query.model.QueryTerminalResponse; import com.amap.api.track.query.model.QueryTrackResponse; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final AMapTrackClient aMapTrackClient = new AMapTrackClient(getApplicationContext()); final long serviceId = 291742; // 这里填入你创建的服务id final String terminal = "user-123"; // 唯一标识某个用户或某台设备的名称 aMapTrackClient.setInterval(5, 30);//报周期设置为30s: aMapTrackClient.setCacheSize(20);//设置缓存 aMapTrackClient.setLocationMode(LocationMode.DEVICE_SENSORS);//定位 final OnTrackLifecycleListener onTrackLifecycleListener = new OnTrackLifecycleListener() { @Override public void onBindServiceCallback(int i, String s) { } @Override public void onStartGatherCallback(int status, String msg) { if (status == ErrorCode.TrackListen.START_GATHER_SUCEE || status == ErrorCode.TrackListen.START_GATHER_ALREADY_STARTED) { Toast.makeText(MainActivity.this, "定位采集开启成功!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "定位采集启动异常!", Toast.LENGTH_SHORT).show(); } } @Override public void onStartTrackCallback(int status, String msg) { if (status == ErrorCode.TrackListen.START_TRACK_SUCEE || status == ErrorCode.TrackListen.START_TRACK_SUCEE_NO_NETWORK || status == ErrorCode.TrackListen.START_TRACK_ALREADY_STARTED) { aMapTrackClient.startGather(this); } else { Toast.makeText(MainActivity.this, "轨迹上报服务服务启动异常," + msg, Toast.LENGTH_SHORT).show(); } } @Override public void onStopGatherCallback(int i, String s) { } @Override public void onStopTrackCallback(int i, String s) { } }; // queryTerminalRequest=new QueryTerminalRequest(serviceId, terminal); // aMapTrackClient.queryTerminal(queryTerminalRequest,DriverNewOrderActivity.this); aMapTrackClient.queryTerminal(new QueryTerminalRequest(serviceId, terminal), new OnTrackListener() { @Override public void onQueryTerminalCallback(QueryTerminalResponse queryTerminalResponse) { if (queryTerminalResponse.isSuccess()) { if (queryTerminalResponse.getTid() <= 0) { aMapTrackClient.addTerminal(new AddTerminalRequest(terminal, serviceId), new OnTrackListener() { @Override public void onQueryTerminalCallback(QueryTerminalResponse queryTerminalResponse) { } @Override public void onCreateTerminalCallback(AddTerminalResponse addTerminalResponse) { if (addTerminalResponse.isSuccess()) { long terminalId = addTerminalResponse.getTid(); aMapTrackClient.startTrack(new TrackParam(serviceId, terminalId), onTrackLifecycleListener); } else { Toast.makeText(MainActivity.this, "请求失败," + addTerminalResponse.getErrorMsg(), Toast.LENGTH_SHORT).show(); } } @Override public void onDistanceCallback(DistanceResponse distanceResponse) { } @Override public void onLatestPointCallback(LatestPointResponse latestPointResponse) { } @Override public void onHistoryTrackCallback(HistoryTrackResponse historyTrackResponse) { } @Override public void onQueryTrackCallback(QueryTrackResponse queryTrackResponse) { } @Override public void onAddTrackCallback(AddTrackResponse addTrackResponse) { } @Override public void onParamErrorCallback(ParamErrorResponse paramErrorResponse) { } }); } else { long terminalId = queryTerminalResponse.getTid(); aMapTrackClient.startTrack(new TrackParam(serviceId, terminalId), onTrackLifecycleListener); } } else { Toast.makeText(MainActivity.this, "请求失败," + queryTerminalResponse.getErrorMsg(), Toast.LENGTH_SHORT).show(); } } @Override public void onCreateTerminalCallback(AddTerminalResponse addTerminalResponse) { } @Override public void onDistanceCallback(DistanceResponse distanceResponse) { } @Override public void onLatestPointCallback(LatestPointResponse latestPointResponse) { } @Override public void onHistoryTrackCallback(HistoryTrackResponse historyTrackResponse) { } @Override public void onQueryTrackCallback(QueryTrackResponse queryTrackResponse) { } @Override public void onAddTrackCallback(AddTrackResponse addTrackResponse) { } @Override public void onParamErrorCallback(ParamErrorResponse paramErrorResponse) { } }); } }
androidMainifest配置文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。