I want to get a MapFragment displayed inside an AlertDialog.
I ended up asking this after reading the following questions [observations between brackets are my results, someone may have got them working but I'm not one to dismiss my amazing talent for screwing things up]:
- http://www.stackoverflow.com/questions/14659705/android-maps-api-v2-in-dialog [not working: map object not instantiated]
- http://www.stackoverflow.com/questions/13733299/initialize-mapfragment-programmatically-with-maps-api-v2 [not working: issues with static methods in a non-static class]
Trying either of those, or a jury-rigged marriage of both, results in either IllegalArgument or NullPointer exceptions. Sometimes I got a whine about being unable to find the view associated with the ID in the extended MapFragment.
The relevant code I've got so far:
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);
// }
}
}