我有一个活动和MapFragment Class。我在Activity中创建了MapFragment对象。当我试图获取mapFragmentObject.getView()时它返回Null.But我已经创建了这样的MapFragment
@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) {
}
}
我从Activity打电话就像 在OnCreate
WOLocator locator = new WOLocator();
getFragmentManager().beginTransaction().add(locator, "Map").commit();
setContentView(getFragmentManager().findFragmentByTag("Map").getView());
我做错了什么?
答案 0 :(得分:1)
这实际上也是我遇到问题的一个问题,因为地图未在您当前尝试请求的位置初始化时导致问题。地图过程比标准视图设置要花费一些时间。
有些听众可以在准备就绪后设置为触发但我发现使用地图片段的onActivityCreated函数非常可靠(我扩展了SupportMapFragment)。
因此,对于您的示例,我会尝试将您的setMapView()函数调用移动到片段的onActivityCreated函数。
希望这有帮助。
答案 1 :(得分:0)
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" />
在java文件中
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);
}
}