请帮助我使用显示标记的齐射来绘制当前位置和从服务器获取位置之间的路线,但我无法在标记上显示折线。我正在从服务器获取经度和纬度的json数组并显示在地图上。当我单击标记时,绘制当前位置与单击的标记之间的路线,并且模式正在行驶。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
String parentID;
String parentToken;
String childID;
Calendar myCalender;
EditText edtFromDate;
EditText editText ;
String toDate;
String fromDate ;
Calendar fromCalender;
DatePickerDialog.OnDateSetListener dateFrom;
DatePickerDialog.OnDateSetListener date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
edtFromDate = (EditText)findViewById(R.id.fromdate);
editText = (EditText) findViewById(R.id.todate);
parentID = getIntent().getStringExtra("parentID");
parentToken = getIntent().getStringExtra("parentToken");
childID = getIntent().getStringExtra("childID");
edtFromDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MapsActivity.this,dateFrom,fromCalender
.get(Calendar.YEAR),fromCalender.get(Calendar.MONTH),
fromCalender.get(Calendar.DAY_OF_MONTH)).show();
}
});
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MapsActivity.this,date,myCalender
.get(Calendar.YEAR),myCalender.get(Calendar.MONTH),
myCalender.get(Calendar.DAY_OF_MONTH)).show();
}
});
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
todate();
fromDate();
toDatePicker();
fromDatePicker();
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https//url";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Server Responce ==>", response);
try {
JSONObject object = new JSONObject(response);
int status = object.getInt("error");
if (status == 0){
JSONArray jsonArray = object.getJSONArray("results");
for (int i=0 ; i<jsonArray.length(); i++ ){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String latitude = jsonObject.getString("lat" );
String longitude = jsonObject.getString("long");
mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble(latitude) , Double.parseDouble(longitude)))
.title(Double.valueOf(latitude).toString() + "," + Double.valueOf(longitude).toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
}else{
Toast.makeText(MapsActivity.this, ""+object.getString("error_message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Server Error ==> ", error.getMessage());
}
});
requestQueue.add(stringRequest);
}
private void fromDatePicker(){
fromCalender = Calendar.getInstance();
dateFrom = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
fromCalender.set(Calendar.YEAR,year);
fromCalender.set(Calendar.MONTH,month);
fromCalender.set(Calendar.DAY_OF_MONTH,dayOfMonth);
updateLabe2();
}
};
}
private void updateLabe2() {
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edtFromDate.setText(sdf.format(fromCalender.getTime()));
}
private void toDatePicker(){
myCalender = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myCalender.set(Calendar.YEAR,year);
myCalender.set(Calendar.MONTH,month);
myCalender.set(Calendar.DAY_OF_MONTH,dayOfMonth);
updateLabel();
}
};
}
private void updateLabel() {
String myFormat = "yyyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
editText.setText(sdf.format(myCalender.getTime()));
}
private void todate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
//c.setTime(sdf.parse(dt));
// c.add(Calendar.DATE, -1);
toDate = sdf.format(c.getTime());
}
private void fromDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
//c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, -1);
c.add(Calendar.MONTH,-1);
fromDate = sdf.format(c.getTime());
}
/*String url = "https://orvisoft.com/childtrackingapi/visits/get/?user="+parentID+"&token="+parentToken+"&children="+childID+"&from_time=2018-04-16&to_time=2018-04-19";*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// String toDate = editText.getText().toString();
// String url = "https://orvisoft.com/childtrackingapi/visits/get/?user="+parentID+"&token="+parentToken+"&children="+childID+"&from_time=2018-04-16&to_time="+toDate+"";
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(100);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(location.getLatitude() + "," + location.getLongitude());
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mMap.addMarker(markerOptions);
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Asking user if explanation is needed
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted. Do the
// contacts-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
} else {
// Permission denied, Disable the functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other permissions this app might request.
// You can add here other case statements according to your requirement.
}
}
}