英文:
Android application for vehicle tracking using GPS
问题
我想开发一个仅使用GPS信号进行车辆跟踪的Android应用程序。它将包括两个组件:
- 一个装在要被跟踪的车辆上的Android设备,上面运行着Android应用程序。该应用程序将每隔1分钟通过短信发送设备的纬度和经度信息。
- 第二个组件将是另一个Android设备,用于接收消息,并由一个Android应用程序读取消息,然后扫描消息以获取纬度和经度信息。一旦获取到纬度和经度信息,将在某个离线地图上绘制位置。
我已经完成了第一个组件,但在绘图部分遇到了困难。我应该如何构建一个可以绘制从短信中接收到的坐标的应用程序?我可以使用Google地图API吗,或者有更好的选择吗?我已经搜索了好几天,但没有找到答案。请帮忙解决。
英文:
I want to develop an android application for vehicle tracking using GPS signals only. It will have two components:
- 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.
- 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
<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"/>
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 = "+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();
}
});
}
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 &&
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 > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
}
case MY_PERMISSIONS_REQUEST_LOCATION: {
if (grantResults.length > 0
&& 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("MissingPermission")
private void getLastLocation(){
if (checkPermissions()) {
if (isLocationEnabled()) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(
new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
message="http://maps.google.com?q="+location.getLatitude()+","+location.getLongitude();
}
}
}
);
} else {
Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
} else {
requestPermissions();
}
}
@SuppressLint("MissingPermission")
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="http://maps.google.com?q="+mLastLocation.getLatitude()+","+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 'com.google.android.gms:play-services-location:17.0.0'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论