所以我有这个活动让用户从微调器中选择一个位置。当用户选择一个时,地图(微调器下的片段)将把摄像机移动到特定目的地。我有这个代码,但我似乎无法使它工作。任何帮助都将非常感激。
TerminalLocationsActivity.java
public class TerminalLocationsActivity extends AppCompatActivity implements OnMapReadyCallback, OnItemSelectedListener {
Spinner spinner;
GoogleMap mMap, mMap2, mMap3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_terminal_locations);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.terminalList, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Context context = getApplicationContext();
CharSequence text = "Tap box to change terminal.";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER, 0,0);
toast.show();
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions().position(new LatLng(10.699160,122.547208)).title("San Pedro Molo Terminal"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.699160, 122.547208), 16));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinSelection = spinner.getSelectedItem().toString();
if(spinSelection.equals("Terminal1")) {
mMap.addMarker(new MarkerOptions().position(new LatLng(10.699160, 122.547208)).title("Terminal1"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.699160, 122.547208), 16));
}
else if (spinSelection.equals("Terminal2")) {
mMap2.addMarker(new MarkerOptions().position(new LatLng(10.699160,122.547208)).title("Terminal2"));
mMap2.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.699160, 122.547208), 16));
}
else if (spinSelection.equals("Terminal3")) {
mMap3.addMarker(new MarkerOptions().position(new LatLng(10.699160,122.547208)).title("Terminal3"));
mMap3.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(10.761685, 122.577042), 16));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//do nothing
}
}