我想在地图开始时创建一个对话框..我在我的地图活动中创建了这个代码,但我不知道如何使它工作我缺少的东西!谢谢
这是我显示地图的活动的完整代码
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
LatLng origin, dest;
String name, name1;
ArrayList<LatLng> MarkerPoints;
TextView ShowDistanceDuration;
Polyline line;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
#.......
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(origin);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4);
// Updating the camera position to the user input latitude and longitude
googleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
googleMap.animateCamera(updateZoom);
Button btnDriving = (Button) findViewById(R.id.btnDriving);
btnDriving.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
build_retrofit_and_get_response("driving");
}
});
Button btnWalk = (Button) findViewById(R.id.btnWalk);
btnWalk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
build_retrofit_and_get_response("walking");
}
});
}
private void addMarker(GoogleMap googleMap, LatLng position, String name) {
// Instantiating MarkerOptions class
MarkerOptions options = new MarkerOptions();
// Setting position for the MarkerOptions
options.position(position);
// Setting title for the MarkerOptions
options.title(name);
// Setting snippet for the MarkerOptions
options.snippet("Latitude:"+position.latitude+",Longitude:"+position.longitude)
googleMap.addMarker(options);
}
// *****************for the dialog to change map*********//
private static final CharSequence[] MAP_TYPE_ITEMS =
{"Road Map", "Hybrid", "Satellite", "Terrain"};
private void showMapTypeSelectorDialog() {
// Prepare the dialog by setting up a Builder.
final String fDialogTitle = "Select Map Type";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(fDialogTitle);
// Find the current map type to pre-check the item representing the
current state.
int checkItem = mMap.getMapType() - 1;
// Add an OnClickListener to the dialog, so that the selection will be
handled.
builder.setSingleChoiceItems(
MAP_TYPE_ITEMS,
checkItem,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// Locally create a finalised object.
// Perform an action depending on which item was
selected.
switch (item) {
case 1:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case 2:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case 3:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
dialog.dismiss();
}
}
);
// Build the dialog and show it.
AlertDialog fMapTypeDialog = builder.create();
fMapTypeDialog.setCanceledOnTouchOutside(true);
fMapTypeDialog.show();
}
}
答案 0 :(得分:1)
只需在Button
文件中添加另一个activity_maps.xml
,然后使用此Button
按呼叫方式showMapTypeSelectorDialog()
更改地图类型。
更新onMapReady()
,如下所示:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(origin);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(4);
// Updating the camera position to the user input latitude and longitude
googleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
googleMap.animateCamera(updateZoom);
Button btnDriving = (Button) findViewById(R.id.btnDriving);
btnDriving.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
build_retrofit_and_get_response("driving");
}
});
Button btnWalk = (Button) findViewById(R.id.btnWalk);
btnWalk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
build_retrofit_and_get_response("walking");
}
});
Button btnChangeMap = (Button) findViewById(R.id.btnChangeMap);
btnChangeMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show map selection dialog
showMapTypeSelectorDialog();
}
});
}
将以下Button
添加到activity_maps.xml
<Button
android:id="@+id/btnChangeMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Map"/>
希望这会有所帮助〜