我正在尝试将Android应用程序设置为超级应用程序,用户可以在其中输入原始位置和目的地位置。然后它将显示路由。
我创建了一个可以从当前位置路由到目的地的应用程序。通过拖动标记来获取目标位置。在这里,Marker正在通过onMarkerDragEnd(Marker marker)方法传递位置。
还有两个edittext框,它们将使用Placepicker进行定位。当我插入地点时,标记出现在地图上。一个用于原点而另一个用于目的地。但路由没有发生。因为我无法传递latlng onMarkerDragEnd(Marker marker)方法中的那些标记。 我需要在onMarkerDragEnd(Marker marker)方法中传递标记的纬度和经度。但问题是我只能传递一个标记的纬度和经度。如何在onMarkerDragEnd(Marker marker)中传递两个标记的latlng? 或者其他任何方式来做到这一点?
这是我的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ORIGIN_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, getActivity());
etOrigin.setText(place.getAddress());
Originlnlt.setText(place.getLatLng().toString());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(getlong(place.getLatLng().toString()),getlat(place.getLatLng().toString())), DEFAULT_ZOOM));
mMap.addMarker(markerOptions.position(new LatLng(getlong(place.getLatLng().toString()),getlat(place.getLatLng().toString()))).title("Origin Position").snippet("I have found me"));
String toastMsg = String.format("Origin Place: %s", place.getName());
Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
}
}
if (requestCode == DESTINATION_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, getActivity());
etDestination.setText(place.getAddress());
Destinationlnlt.setText(place.getLatLng().toString());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(getlong(place.getLatLng().toString()),getlat(place.getLatLng().toString())), DEFAULT_ZOOM));
mMap.addMarker(markerOptions
.position(new LatLng(getlong(place.getLatLng().toString()),
getlat(place.getLatLng().toString())))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).title(place.getName().toString()).snippet(place.getAddress().toString()).title("Destination Position").snippet("I want to go"));
String toastMsg = String.format("Destination Place: %s", place.getName());
Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
}
}
//TODO
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = (MapView) mView.findViewById(R.id.map);
if (mMapView != null) {
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
btnFindPath = (Button) mView.findViewById(R.id.btnFindPath);
etOrigin = (EditText) mView.findViewById(R.id.etOrigin);
etDestination = (EditText) mView.findViewById(R.id.etDestination);
Originlnlt = (TextView) mView.findViewById(R.id.originL);
Destinationlnlt = (TextView) mView.findViewById(R.id.desL);
btnFindPath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//sendRequest();
Object dataTransfer[] = new Object[2];
dataTransfer = new Object[3];
String url = getDirectionsUrl();
GetDirectionsData getDirectionsData = new GetDirectionsData();
dataTransfer[0] = mMap;
dataTransfer[1] = url;
dataTransfer[2] = new LatLng(end_latitude, end_longitude);
//dataTransfer[2] = new LatLng( 22.34730250000000,91.83745703125001);
getDirectionsData.execute(dataTransfer);
}
});
etOrigin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// Always use a TextKeyListener when clearing a TextView to prevent android
// warnings in the log
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(getActivity()), ORIGIN_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
}
});
etDestination.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// Always use a TextKeyListener when clearing a TextView to prevent android
// warnings in the log
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(getActivity()), DESTINATION_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
}
});
}
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mMap = googleMap;
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
mMap.setOnMarkerDragListener(this);
mMap.setOnMarkerClickListener(this);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}