我想在AlertDialog中显示一个MapFragment。
在阅读了以下问题后,我问了这个问题[括号之间的观察结果是我的结果,有人可能已经让他们工作了,但我不是一个否认我把事情搞砸的惊人天赋的人]:
- http://www.stackoverflow.com/questions/14659705/android-maps-api-v2-in-dialog[不工作:地图对象未实例化]
- http://www.stackoverflow.com/questions/13733299/initialize-mapfragment-programmatically-with-maps-api-v2[不工作:静态方法在非静态类中的问题]
尝试其中任何一个,或者临时将两者结合起来,都会导致IllegalArgument或NullPointer异常。有时我会抱怨无法在扩展的MapFragment中找到与ID相关的视图。
目前我得到的相关代码:
public class PropDetailActivity extends Activity
{
static final int PICK_REQUEST = 70003;
private static final int TABLE_PROP_TYPES = 13,
ADD_ID = Menu.FIRST + 1,
DELETE_ID = Menu.FIRST + 3;
Button btnLocation;
private LocationManager daLocationManager;
private String daProvider;
private double daCurrentLat, daCurrentLong;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.prop_detail);
btnLocation = (Button) findViewById(R.id.btnLocation);
btnLocation.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
openGps();
}
});
doMapGubbinz();
}
private void openGps()
{
LayoutInflater inflater = LayoutInflater.from(this);
LatLng here = new LatLng(daCurrentLat, daCurrentLong);
MapFragment fragment = PreparedMapFragment.newInstance(here);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment)
.commit();
//daMap = fragment.getMap();
View gps = inflater.inflate(R.layout.location_gps, null);
new AlertDialog.Builder(this)
.setTitle("Property location")
.setView(gps)
.show();
// Marker currentLoc = daMap.addMarker(new MarkerOptions().position(here).title("Ubicación actual"));
//
// daMap.moveCamera(CameraUpdateFactory.newLatLngZoom(here, 15));
//
// daMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
public void doMapGubbinz()
{
daLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
daProvider = daLocationManager.getBestProvider(criteria, false);
daLocationManager.requestLocationUpdates(daProvider, 400, 1, this);
Location location = daLocationManager.getLastKnownLocation(daProvider);
if (location != null)
{
System.out.println("Provider " + daProvider + " has been selected.");
onLocationChanged(location);
}
daCurrentLat = location.getLatitude();
daCurrentLong = location.getLongitude();
}
static public class PreparedMapFragment extends MapFragment
{
static MapFragment frag;
public PreparedMapFragment()
{
super();
}
public static MapFragment newInstance(LatLng pos)
{
frag = new MapFragment();
UiSettings settings = frag.getMap().getUiSettings();
settings.setAllGesturesEnabled(false);
settings.setMyLocationButtonEnabled(false);
frag.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(pos, 16));
frag.getMap().addMarker(new MarkerOptions().position(pos).title("Ubicación actual"));
frag.getMap().animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
return frag;
}
// @Override
// public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2)
// {
// View v = super.onCreateView(arg0, arg1, arg2);
// initMap();
// return v;
// }
//
// private void initMap()
// {
// UiSettings settings = getMap().getUiSettings();
//
// settings.setAllGesturesEnabled(false);
// settings.setMyLocationButtonEnabled(false);
//
// getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(daCurrentPos, 16));
//
// getMap().addMarker(new MarkerOptions().position(daCurrentPos).title("Ubicación actual"));
//
// getMap().animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
// }
}
}
你可以在一个对话框中显示一个地图片段
public class DialogMapFragment extends DialogFragment {
private SupportMapFragment fragment;
public DialogMapFragment() {
fragment = new SupportMapFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mapdialog, container, false);
getDialog().setTitle("");
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapView, fragment).commit();
return view;
}
public SupportMapFragment getFragment() {
return fragment;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp" >
<FrameLayout
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>`
通过new DialogMapFragment().show(getFragmentManager(),"tag")显示对话框;你可以用任何你需要的按钮来定制对话片段