Android应用程序,使用GPS进行车辆追踪

huangapple go评论72阅读模式
英文:

Android application for vehicle tracking using GPS

问题

我想开发一个仅使用GPS信号进行车辆跟踪的Android应用程序。它将包括两个组件:

  1. 一个装在要被跟踪的车辆上的Android设备,上面运行着Android应用程序。该应用程序将每隔1分钟通过短信发送设备的纬度和经度信息。
  2. 第二个组件将是另一个Android设备,用于接收消息,并由一个Android应用程序读取消息,然后扫描消息以获取纬度和经度信息。一旦获取到纬度和经度信息,将在某个离线地图上绘制位置。

我已经完成了第一个组件,但在绘图部分遇到了困难。我应该如何构建一个可以绘制从短信中接收到的坐标的应用程序?我可以使用Google地图API吗,或者有更好的选择吗?我已经搜索了好几天,但没有找到答案。请帮忙解决。

英文:

I want to develop an android application for vehicle tracking using GPS signals only. It will have two components:

  1. An android device with android application that will be in the vehicle to be tracked. It will send lat and long of the device using text message after the interval of 1 minute.
  2. The second component will be an android device where the message will be delivered and the message will be then read by an android application which will scan the message for lat and long. Once the lat and long are found the location will be plotted on some offline map.

I have completed the first component and i am stuck with the plotting part. How can i build an app that can plot the coordinates received in a text messages.
Can I use the google maps API, or there is something better for this? I have been searching for days but with no luck. Please help

答案1

得分: 1

这是您提供的代码的翻译部分:

我会告诉您如何发送包含当前位置纬度和经度的文本消息。

在您的Android清单中允许这些权限:

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

然后这将是您的活动:

public class MainActivity extends AppCompatActivity {
    private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
    Button sendBtn;

    String phoneNo = "+923044075653";

    String message;
    LocationManager locationManager;
    FusedLocationProviderClient mFusedLocationClient;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBtn = (Button) findViewById(R.id.btnSendSMS);
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        getLastLocation();
        sendBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                sendSMSMessage();
            }
        });
    }

    // 其他方法和回调
}

根据这段代码,单击按钮将向您的移动网络发送文本消息。

并且在您的Gradle文件中包括以下库:

implementation 'com.google.android.gms:play-services-location:17.0.0'
英文:

I will tell u how to send the text message to send current location latitude and longitude

In your android manifest allow these permissions

&lt;uses-permission android:name=&quot;android.permission.SEND_SMS&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot;/&gt;

Then this will be your activity

public class MainActivity extends AppCompatActivity {
    private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
    Button sendBtn;

    String phoneNo = &quot;+923044075653&quot;;

    String message;
    LocationManager locationManager;
    FusedLocationProviderClient mFusedLocationClient;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendBtn = (Button) findViewById(R.id.btnSendSMS);
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        getLastLocation();
        sendBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                sendSMSMessage();
            }
        });



    }
    protected void sendSMSMessage() {







        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.SEND_SMS)) {
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.SEND_SMS},
                        MY_PERMISSIONS_REQUEST_SEND_SMS);
            }
        }
    }

    private boolean checkPermissions() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &amp;&amp;
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        return false;
    }

    private void requestPermissions() {
        ActivityCompat.requestPermissions(
                this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_LOCATION
        );
    }

    private boolean isLocationEnabled() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(
                LocationManager.NETWORK_PROVIDER
        );
    }
    @Override
    public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_SEND_SMS: {
                if (grantResults.length &gt; 0
                        &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNo, null, message, null, null);
                    Toast.makeText(getApplicationContext(), &quot;SMS sent.&quot;,
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            &quot;SMS faild, please try again.&quot;, Toast.LENGTH_LONG).show();
                    return;
                }
            }
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                if (grantResults.length &gt; 0
                        &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    getLastLocation();
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
            }


        }

    }
    @Override
    public void onResume(){
        super.onResume();
        if (checkPermissions()) {
            getLastLocation();
        }

    }

    @SuppressLint(&quot;MissingPermission&quot;)
    private void getLastLocation(){
        if (checkPermissions()) {
            if (isLocationEnabled()) {
                mFusedLocationClient.getLastLocation().addOnCompleteListener(
                        new OnCompleteListener&lt;Location&gt;() {
                            @Override
                            public void onComplete(@NonNull Task&lt;Location&gt; task) {
                                Location location = task.getResult();
                                if (location == null) {
                                    requestNewLocationData();
                                } else {

                                    message=&quot;http://maps.google.com?q=&quot;+location.getLatitude()+&quot;,&quot;+location.getLongitude();
                                }
                            }
                        }
                );
            } else {
                Toast.makeText(this, &quot;Turn on location&quot;, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        } else {
            requestPermissions();
        }


    }

    @SuppressLint(&quot;MissingPermission&quot;)
    private void requestNewLocationData(){

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(0);
        mLocationRequest.setFastestInterval(0);
        mLocationRequest.setNumUpdates(1);

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        mFusedLocationClient.requestLocationUpdates(
                mLocationRequest, mLocationCallback,
                Looper.myLooper()
        );

    }

    private LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            Location mLastLocation = locationResult.getLastLocation();
            message=&quot;http://maps.google.com?q=&quot;+mLastLocation.getLatitude()+&quot;,&quot;+mLastLocation.getLongitude();
        }
    };

According to this code by clicking on button a text message will be sent to your mobile network

and in your gradle file include these libraries

implementation &#39;com.google.android.gms:play-services-location:17.0.0&#39;

huangapple
  • 本文由 发表于 2020年1月6日 17:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定