溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android如何實(shí)現(xiàn)地圖軌跡

發(fā)布時(shí)間:2021-04-16 09:30:39 來(lái)源:億速云 閱讀:287 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下Android如何實(shí)現(xiàn)地圖軌跡,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(nèi)容如下

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;  // 這里填入你創(chuàng)建的服務(wù)id
        final String terminal = "user-123";   // 唯一標(biāo)識(shí)某個(gè)用戶(hù)或某臺(tái)設(shè)備的名稱(chēng)

        aMapTrackClient.setInterval(5, 30);//報(bào)周期設(shè)置為30s:
        aMapTrackClient.setCacheSize(20);//設(shè)置緩存
        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, "定位采集開(kāi)啟成功!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "定位采集啟動(dòng)異常!", 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, "軌跡上報(bào)服務(wù)服務(wù)啟動(dòng)異常," + 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, "請(qǐng)求失敗," + 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, "請(qǐng)求失敗," + 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配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.amapdemo">

    <!--允許程序打開(kāi)網(wǎng)絡(luò)套接字-->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--允許程序設(shè)置內(nèi)置sd卡的寫(xiě)權(quán)限-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允許程序讀寫(xiě)手機(jī)狀態(tài)和身份-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!--允許程序訪(fǎng)問(wèn)CellID或WiFi熱點(diǎn)來(lái)獲取粗略的位置-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>

    <!--用于進(jìn)行網(wǎng)絡(luò)定位-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!--用于訪(fǎng)問(wèn)GPS定位-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!--用于獲取運(yùn)營(yíng)商信息,用于支持提供運(yùn)營(yíng)商信息相關(guān)的接口-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <!--用于訪(fǎng)問(wèn)wifi網(wǎng)絡(luò)信息,wifi信息會(huì)用于進(jìn)行網(wǎng)絡(luò)定位-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <!--用于獲取wifi的獲取權(quán)限,wifi信息會(huì)用來(lái)進(jìn)行網(wǎng)絡(luò)定位-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <!--用于申請(qǐng)調(diào)用A-GPS模塊-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>

    <!--用于進(jìn)行網(wǎng)絡(luò)定位-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <!--用于訪(fǎng)問(wèn)GPS定位-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <!--獲取運(yùn)營(yíng)商信息,用于支持提供運(yùn)營(yíng)商信息相關(guān)的接口-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <!--用于訪(fǎng)問(wèn)wifi網(wǎng)絡(luò)信息,wifi信息會(huì)用于進(jìn)行網(wǎng)絡(luò)定位-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <!--這個(gè)權(quán)限用于獲取wifi的獲取權(quán)限,wifi信息會(huì)用來(lái)進(jìn)行網(wǎng)絡(luò)定位-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <!--用于訪(fǎng)問(wèn)網(wǎng)絡(luò)-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <!--用于讀取手機(jī)當(dāng)前的狀態(tài)-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!--寫(xiě)入擴(kuò)展存儲(chǔ),向擴(kuò)展卡寫(xiě)入數(shù)據(jù),用于寫(xiě)入緩存定位數(shù)據(jù)-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <!--用于申請(qǐng)調(diào)用A-GPS模塊-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <!--用于申請(qǐng)獲取藍(lán)牙信息進(jìn)行室內(nèi)定位-->
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AMapDemo">
<!--        <service android:name="com.amap.api.track.AMapTrackService" />-->
        <service android:name="com.amap.api.location.APSService" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <meta-data android:name="com.amap.api.v2.apikey"
            android:value="388784de5a9c159ac13b5e3db130c0ad"/>
        <service android:name="com.amap.api.track.AMapTrackService" />
    </application>

</manifest>

以上是“Android如何實(shí)現(xiàn)地圖軌跡”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI