I read the tutorial Google Maps V2 and implement all, but when I run application I don't see the markers and I don't understand why. I want add a marker with a position 'PLACE' and I add it, but don't show.
I think that the problem is in this line:
mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
Because when I add 'mapOptions()' in 'MapFragment.newInstance' the map show position 'PLACE', but if I don't add this options, the map appears normal.
Someone can help? Thanks and sorry about the english.
My code:
Frag_Map.java
public class Frag_Map extends Activity {
private GoogleMap mMap;
private static final LatLng PLACE = new LatLng(38.977,-9.4185);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xmlfile);
MapFragment mMapFragment = MapFragment.newInstance(mapOptions());
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map, mMapFragment);
fragmentTransaction.commit();
setUpMapIfNeeded();
}
private GoogleMapOptions mapOptions()
{
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_HYBRID)
.compassEnabled(true)
.rotateGesturesEnabled(true)
.scrollGesturesEnabled(true)
.tiltGesturesEnabled(true)
.zoomGesturesEnabled(true)
.zoomControlsEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(PLACE)
.zoom((float) 17.5)
.build();
options.camera(cameraPosition);
return options;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
mMap.addMarker(new MarkerOptions()
.position(new LatLng(38.977,-9.4185))
.title("Hello world")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.mapaovelay1))
.visible(true));
}
}
}
xmlfile.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>