如何在Android中获取当前位置,并在当前位置和触摸位置之间绘制路径或线。
答案 0 :(得分:1)
要了解您当前所在的位置,您需要获取当前交通的纬度和经度。
public void getLatiLongi()
{
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
flati = location.getLatitude();
flongi = location.getLongitude();
System.out.println("Hello....................+lati+longi");
}
}
@Override
protected void onStart() {
super.onStart();
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
gpsAlert();
}
}
public void gpsAlert(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Enable GPS");
alertDialogBuilder
.setMessage("Yes")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
enableLocationSettings();
dialog.cancel();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
private void enableLocationSettings() {
Intent settingsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}
之后使用标记
https://developers.google.com/maps/documentation/android/marker。
您需要目的地的纬度和经度。
https://developers.google.com/maps/documentation/android/shapes#polylines
使用折线绘制从您的位置到目的地的路径
Answer : Draw path between two points using Google Maps Android API v2。这个链接对这个主题有很好的解释。