I have a master class which creates two fragment classes. One of them is MapDisplay().java
I want to display a map in this class. Here is my code for MapDisplay()
public class MapDisplay extends Fragment implements OnMapClickListener{
private GoogleMap googlemap;
LatLng GAN = new LatLng(34.4305556,74.9250000);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.maplayout, container, false);
try
{
if(googlemap==null)
{
googlemap=
((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map2)).getMap();
googlemap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googlemap.setMyLocationEnabled(true);
googlemap.getUiSettings().setMyLocationButtonEnabled(true);
googlemap.getUiSettings().setZoomControlsEnabled(false);
}
if(googlemap!=null)
{
Marker m = googlemap.addMarker(new MarkerOptions().position(GAN).title("Gangbal"));
m.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(GAN, 15));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(10),2000,null);
googlemap.setOnMapClickListener(this);
}
else
{
Toast.makeText(getActivity(), "WELL! HELLO THERE", Toast.LENGTH_LONG).show();
}
} catch(Exception e)
{
e.printStackTrace();
}
return rootView;
}
@Override
public void onMapClick(LatLng arg0) {
Bundle args1 = new Bundle();
args1.putParcelable("LOCATION", GAN);
Intent a = new Intent(getActivity(),FullScreenMap.class);
a.putExtra("bundle", args1);
a.putExtra("title", "Gangbal");
startActivity(a);
}
}
And here's my XML layout file maplayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
This class however, doesn't display the map I want to. Just a world map with no markers. Also clicking on this map doesn't work.
I'm creating the MapDisplay() fragment class in another class
FragmentPagerAdapter tabadapter = new TabAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.tabpager);
pager.setAdapter(tabadapter);
class TabAdapter extends FragmentPagerAdapter{
public TabAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int m) {
switch(m)
{
case 0:
return new MapDisplay();
case 1:
return new Description();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length];
}
@Override
public int getCount() {
return CONTENT.length;
}
}
So I guess MapDisplay() should extend Fragment, right?? What am I doing wrong? Please help!