我有一个名为PlacesFragment的片段,它有一个链接到另一个名为RoutePlanerFragment的片段的按钮,该片段有一个谷歌地图和一个用于选择地点的微调器。当我单击PlacesFragment上的按钮时,我想让路线自动显示在地图上,必须从微调器中选择。
以下是PlacesFragment按钮的代码
Button btnToRoute = (Button) rootView.findViewById(R.id.btnToRoute);
btnToRoute.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View rootView){
RoutePlanerFragment routePlanerFragment = new RoutePlanerFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.content, routePlanerFragment, routePlanerFragment.getTag()).commit();
这是我的RoutePlanerFragment
的地图和微调部分@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_route_planer, container, false);
String[] values =
{"Route berechnen", "Campus Heilbronn Sontheim", "Campus Schwäbisch Hall", "Campus Künzelsau", "Bildungscampus Nord"};
Spinner spinner = (Spinner) rootView.findViewById(R.id.routeSpinner);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return rootView;
}
mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastKnownLocation == null) {
lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
mapView = (MapView) rootView.findViewById(R.id.routePlanerMapView);
mapView.onCreate(savedInstanceState);
mapView.onResume(); // needed to get the map to display immediately mMapView.
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady (GoogleMap mMap){
googleMap = mMap;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude()), 15));
mMap.addMarker(new MarkerOptions().position(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())).title("Du bist hier!").snippet("Text"));
}
});
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
lastKnownLocation = location;
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), 15));
googleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Du bist hier!").snippet("Text"));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
LatLng destination = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
switch (adapterView.getSelectedItem().toString()) {
case "Campus Heilbronn Sontheim":
destination = new LatLng(49.122123, 9.211115);
break;
case "Campus Schwäbisch Hall":
destination = new LatLng(49.112536,9.743618);
break;
case "Campus Künzelsau":
destination = new LatLng(49.275552, 9.712164);
break;
case "Bildungscampus Nord":
destination = new LatLng(49.147784, 9.217433);
break;
default: return;
}
final LatLng origin = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
GoogleDirection.withServerKey("AIzaSyD89DmgdibFa3PzNFfGkfB6S7c428Gvo9c")
.from(origin)
.to(destination)
.language(Language.GERMAN)
.execute(new DirectionCallback() {
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if(direction.isOK()) {
googleMap.clear();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastKnownLocation.getLatitude(),lastKnownLocation.getLongitude()), 15));
googleMap.addMarker(new MarkerOptions().position(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())).title("Du bist hier!").snippet("Text"));
Route route = direction.getRouteList().get(0);
Leg leg = route.getLegList().get(0);
String wayPointHtml = "";
for (Step step : leg.getStepList()) {
Log.d("BESCHREIBUNG", step.getHtmlInstruction().toString());
wayPointHtml += step.getHtmlInstruction().toString() + "<br/><br/>";
}
WebView webView = (WebView) rootView.findViewById(R.id.routeWebView);
webView.loadData(wayPointHtml, "text/html", "utf-8");
Info distanceInfo = leg.getDistance();
Info durationInfo = leg.getDuration();
TextView routeInfo = (TextView) rootView.findViewById(R.id.routeInfo);
routeInfo.setText("Entfernung: " + distanceInfo.getText() + "Dauer: " + durationInfo.getText());
ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
PolylineOptions polylineOptions = DirectionConverter.createPolyline(getContext(), directionPositionList, 5, Color.RED);
googleMap.addPolyline(polylineOptions);
} else {
}
}
@Override
public void onDirectionFailure(Throwable t) {
}
});
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
);
return rootView;
}
fragment_places.xml上显示的值存储在DummyContent.java中
这是fragment_places.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.ensartok.rossappensar.PlacesFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="417dp" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Campus Sontheim"
android:textSize="14sp" />
<TextView
android:id="@+id/street"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Max-Planck-Straße 39"
android:textSize="14sp" />
<TextView
android:id="@+id/zipplace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="74081 Heilbronn"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="38dp"
android:text="Route berechnen"
android:textSize="14sp" />
</LinearLayout>
这就是DummyContent.java中值的部分
public static final Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
static {
DummyItem standort1 = new DummyItem("1", "HHN Campus Sontheim", "Max-Planck-Straße 39","74081", "Heilbronn","49.122123", "9.211115");
addItem(standort1);
DummyItem standort2 = new DummyItem("2", "Campus Schwäbisch Hall", "Ziegeleiweg 4","74523", "Schwäbisch Hall","49.112536", "9.743618");
addItem(standort2);
DummyItem standort3 = new DummyItem("3",
"Reinhold-Würth-Hochschule – Campus Künzelsau",
"Daimlerstraße 35","74653",
"Künzelsau",
"49.275552", "9.712164");
addItem(standort3);
DummyItem standort4 = new DummyItem("4",
"Bildungscampus Nord",
"Bauteil N / Nr. 12","74076",
"Heilbronn",
"49.147784", "9.217433");
addItem(standort4);
}
很抱歉这个问题涉及很多代码,但我想你们需要能够回答我的问题。
再一次。
如果您按下PlacesFragment上的按钮,您将进入RoutePlanerFragment,现在我希望路线自动到达我在PlacesFragment中按下按钮的目的地。
希望你们得到我想要解释的内容
答案 0 :(得分:0)
嗨根据我的理解,您可以通过此
将数据从一个片段发送到另一个片段这会将数据添加到捆绑
Bundle bundle = new Bundle();
bundle.putString("latitude", latitude);
bundle.putString("longitude", longitude);
bundle.putString("board_id", board_id);
Fragment_A fragment = new Fragment_A();
fragment.setArguments(bundle);
这将从Fragment_A
中获取数据Bundle bundle=getArguments();
String latitude = getArguments().getString("latitude")
您也可以发送ArrayList。