Reputation: 1640
I have an activity and MapFragment Class.I have created MapFragment Object in Activity.When I tried to get mapFragmentObject.getView() it is returning Null.But i have created MapFragment like this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
view = inflater.inflate(R.layout.activity_main, container, false);
mapView = ((MapView) view.findViewById(R.id.map));
mapView.onCreate(savedInstanceState);
setMapView();
return view;
}
public void setMapView() {
try {
map = mapView.getMap();
map.clear();
LatLngBounds.Builder builder = new LatLngBounds.Builder();
MapData mapData = MapData.getInstance();
Double[][] latlang = mapData.getLatlang();
String[] WONum = mapData.getWOnum();
marker = new Marker[mapData.getLatlang().length];
for (int i = 0; i < mapData.getLatlang().length; i++) {
this.marker[i] = map.addMarker(new MarkerOptions()
.position(new LatLng(latlang[i][0], latlang[i][1]))
.title(" ").snippet(" " + WONum[i] + "\n"));
builder.include(this.marker[i].getPosition());
}
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 60,
60, 1);
map.animateCamera(cu);
map.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
if (position.zoom > 12.0f) {
map.animateCamera(CameraUpdateFactory.zoomTo(12.0f));
} else if (position.zoom < 2.0f) {
map.animateCamera(CameraUpdateFactory.zoomTo(4.0f));
}
}
});
} catch (Exception e) {
}
}
And i am calling from Activity is like in OnCreate
WOLocator locator = new WOLocator();
getFragmentManager().beginTransaction().add(locator, "Map").commit();
setContentView(getFragmentManager().findFragmentByTag("Map").getView());
What i am doing wrong?
Upvotes: 1
Views: 737
Reputation: 5121
in layout activity_main.xml
<com.google.android.gms.maps.MapView
android:id="@+id/mapEditView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
in java file
MapView mapView;
GoogleMap map;
LatLng CENTER = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
view = inflater.inflate(R.layout.activity_main, container,
false);
CENTER = new LatLng(latitudeDoubleValue, longitudeDoubleValue);
mapView = (MapView) view.findViewById(R.id.mapEditView);
mapView.onCreate(savedInstanceState);
setMapView();
}
private void setMapView() {
if (mapView != null) {
locationManager = ((LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE));
locListener = new MyLocationListener();
Boolean localBoolean = Boolean.valueOf(locationManager
.isProviderEnabled("network"));
if (localBoolean.booleanValue()) {
CENTER = new LatLng(latitudeDouble, longitudeDouble);
GetCityNameFromLatitudeLongitude getCityName = new GetCityNameFromLatitudeLongitude(
getActivity());
locationString = getCityName.getCityNameFromLatitudeLongitude(
CENTER.latitude, CENTER.longitude);
} else {
}
map = mapView.getMap();
if (map == null) {
Log.d("----------->>>",
"Map Fragment Not Found or no Map in it!!");
return;
}
map.clear();
try {
map.addMarker(new MarkerOptions().position(CENTER)
.title(locationString).snippet(""));
} catch (Exception e) {
e.printStackTrace();
}
map.setIndoorEnabled(true);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.zoomTo(5));
if (CENTER != null) {
map.animateCamera(CameraUpdateFactory.newLatLng(CENTER), 1750,
null);
}
// add circle
CircleOptions circle = new
CircleOptions();
circle.center(CENTER).fillColor(Color.BLUE).radius(10);
map.addCircle(circle);
// map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.setOnMapClickListener(this);
}
}
Upvotes: 0
Reputation: 1156
This is actually one I have had a problem with too, the issue is caused as the map is not initialised at the point you are currently trying to request it. The map process takes a while longer than a standard view set up.
There are some listeners you can set up to fire once it is ready but I have found it quite reliable to use the onActivityCreated function of your map fragment (mine extends SupportMapFragment).
So for your example I would try moving your setMapView() function call to the onActivityCreated function of your fragment.
Hope this helps.
Upvotes: 1