我目前正在使用Google Maps API v2制作校园地图。 我目前有10个地图标记。他们每个人都有自己的信息窗口。 我想要发生的是,当用户点击信息窗口时,它会显示Listview自定义对话框。我想使用if else语句,但我不知道如何构建,因为我没有在互联网上找到任何例子。
这是我的对话活动
public class AdminDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.layers)
.setItems(R.array.layer_options, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
}
我有一些样本标记......
public void addMarkersToMap() {
Marker cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
Marker adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
Marker casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
我关心的是如何使用此代码构造if else语句或switch case语句..这是调用自定义对话框的地方..
adminDialog = new AdminDialog();
adminDialog.show(getFragmentManager(), "custom-tag-goes-here");
答案 0 :(得分:2)
将您的标记声明为字段,而不是本地变量
Marker cmumarker , adminmarker, casmarker;
// You can also keep the ids
String cmumarkerId , adminmarkerId, casmarkerID;
public void addMarkersToMap() {
cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
cmumarkerId=cmumarker.getID();
adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
adminmarkerId=adminmarker.getID();
casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
casmarkerId=casmarker.getID();
}
将点击监听器设置为标记,例如在您创建地图后,或在addMarkersToMap()
map.setOnMarkerClickListener(this);
覆盖onMarkerClick(Marker marker)
功能,并做出决定
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.getId().equals(cmumarkerId)) {
//do whatever you want
return true;
}
if (marker.getId().equals(adminmarkerId)) {
//do whatever you want
return true;
}
if (marker.getId().equals(casmarkerId)) {
//do whatever you want
return true;
}
return false;
}
此外,如果您希望信息窗口是可引用的,而不是标记,那么
public void onInfoWindowClick(Marker marker)
而不是onMarkerClick map.setOnInfoWindowClickListener(this);
而不是map.setOnMarkerClickListener(this)对侦听器进行签名; 更新:
如果您有许多标记,您可以寻找更好的方法来保存ID。有些人通过引用直接比较标记(即if(marker == adminmarker {...})但有时它恰好是具有相同值的不同对象,因此id是比较它们的最安全方式
答案 1 :(得分:0)
更新答案.. 对Carlos Robles的信用 ..
Marker cmumarker, adminmarker, casmarker;
String PREVIOUS_ID;
public void addMarkersToMap() {
cmumarker = map.addMarker(new MarkerOptions()
.position(cmu)
.title("Central Mindanao University")
.snippet("Population: 6,143"));
cmumarker.showInfoWindow();
adminmarker = map.addMarker(new MarkerOptions()
.position(admin)
.title("Central Mindanao University Administration Building")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
casmarker = map.addMarker(new MarkerOptions()
.position(cas)
.title("College of Arts and Sciences")
.snippet("Population: 1,234")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
PREVIOUS_ID=adminmarker.getId();
map.setOnInfoWindowClickListener(this);
}
这是一个示例,其中自定义对话框只能由管理员调用..
@Override
public void onInfoWindowClick(Marker marker) {
AdminDialog adminDialog;
if (marker.getId().equals(PREVIOUS_ID)) {
adminDialog = new AdminDialog();
adminDialog.show(getFragmentManager(), "custom-tag-goes-here");
}
}