首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapFragment返回空值

MapFragment返回空值
EN

Stack Overflow用户
提问于 2014-08-07 22:15:41
回答 2查看 693关注 0票数 1

我有一个activity和MapFragment类。我在Activity.When中创建了MapFragment对象,我尝试获取mapFragmentObject.getView(),它返回Null.But。我创建了MapFragment,如下所示

代码语言:javascript
复制
@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里一样

代码语言:javascript
复制
WOLocator locator = new WOLocator();
getFragmentManager().beginTransaction().add(locator, "Map").commit();
setContentView(getFragmentManager().findFragmentByTag("Map").getView());

我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2014-08-07 23:32:52

这实际上也是我遇到的一个问题,这个问题是由于地图在你当前试图请求它的时候没有初始化而引起的。映射过程比标准视图设置需要更长的时间。

有一些监听器可以设置为一旦准备好就触发,但我发现使用映射片段(我的扩展了SupportMapFragment)的onActivityCreated函数非常可靠。

因此,对于您的示例,我将尝试将您的setMapView()函数调用移动到您的片段的onActivityCreated函数。

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2014-08-14 12:54:59

代码语言:javascript
复制
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文件中

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25184863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档