我想在 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);
// }
}
}