0

我有一个显示良好的 GoogleMap(在 SupportMapFragment 内),并将 GoogleMapOptions 用于目标相机位置。但是,我无法向 GoogleMap 添加标记/折线。下面是创建地图的方法:

private void createMap(List<LatLng> latLngs) {

    if(map == null) {
        GoogleMapOptions options = new GoogleMapOptions();
        mapFragment = SupportMapFragment.newInstance(options);
        map = mapFragment.getMap();

        float zoom = 13;
        CameraPosition cameraP = new CameraPosition(latLngs.get(0), zoom, 0, 0);
        options.camera(cameraP);


        //TODO MAP IS NULL - SORT OUT!
        // check it has been instantiated
        if (map != null) {
            Log.d(TAG, "map is not null");
            map.clear();
            //Calculate target zoom, based on trip size
            map.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraP));
            // Add LatLngs to polyline

            PolylineOptions poly = new PolylineOptions().color(Color.RED);
            MarkerOptions startMarker = new MarkerOptions()
                    .position(latLngs.get(0)).title("Start");
            MarkerOptions endMarker = null;
            if(latLngs.size() > 1) {
             endMarker = new MarkerOptions().position(
                    latLngs.get(latLngs.size() - 1)).title("End");  
            }

            for (LatLng latLng : latLngs) {
                poly.add(latLng);
            }

            map.addPolyline(poly);
            map.addMarker(startMarker);
            map.addMarker(endMarker);

        }


        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.trip_summary_map_container, mapFragment);
        ft.commit();
    }
}

正如您从内联注释中看到的那样,地图仍然为空(尽管它正在显示和使用选项)。只是不能添加东西。我假设我没有正确实例化它?

Activity 扩展了 FragmentActivity,我已经设置了使用 Maps API 所需的所有东西。

感谢您的任何帮助。

4

3 回答 3

2

编辑:我已经用我现在更喜欢使用的解决方案发布了一个新的答案。

几天前我遇到了同样的问题,我解决了扩展SupportMapFragment类,以便在地图最终准备好后执行回调方法。

public class ExtendedSupportMapFragment extends SupportMapFragment {

    public static interface MapReadyListener {
        public void mapIsReady(GoogleMap map);
    }

    @Deprecated
    public static SupportMapFragment newInstance() {
        return null;
    }

    @Deprecated
    public static SupportMapFragment newInstance(GoogleMapOptions options) {
        return null;
    }

    public static ExtendedSupportMapFragment newInstance(MapReadyListener mapReadyListener) {
        ExtendedSupportMapFragment fragment = new ExtendedSupportMapFragment();
        fragment.mapReadyListener = mapReadyListener;

        return fragment;
    }

    private MapReadyListener mapReadyListener;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mapReadyListener != null)
            mapReadyListener.mapIsReady(getMap());
    }

}

然后你只需要做这样的事情:

public class RutaMapaFragment extends SherlockFragment implements ExtendedSupportMapFragment.MapReadyListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragmentMapa = ExtendedSupportMapFragment.newInstance(RutaMapaFragment.this);
    }

    ...

    @Override
    public void mapIsReady(GoogleMap map) {
        //Do whatever you want with your map.
    }

}
于 2013-03-11T09:54:26.860 回答
1

所以,时间已经过去了。事实是,我不再使用之前答案中的解决方案,而是更喜欢使用 a ViewTreeObserver。以下代码显示了一个相当简单的片段,其中SupportMapFragment添加了 a。

该方法createMap()添加SupportMapFragment然后执行setupMap(),但只能通过一个OnGlobalLayoutListener基本上在地图实际准备好后执行的方法。当然,这个监听器会被立即删除——没有必要再保留它了。

public class MyMapFragment extends Fragment {

    private View mMapContainer;
    private SupportMapFragment mMapFragment;
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(/* ... */);
        // ...

        mMapContainer = view.findViewById(R.id.map_fragment_container);
        createMap();

        return view;
    }

    private void createMap() {
        mMapFragment = new SupportMapFragment();

        getFragmentManager().beginTransaction()
                .replace(R.id.map_fragment_container, mMapFragment)
                .commit();

        mMapContainer.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    @Override
                    public void onGlobalLayout() {
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                            mMapContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        } else {
                            mMapContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }

                        mMap = mMapFragment.getMap();
                        setupMap();
                    }

                });
    }

    private void setupMap() {
        mMap.setMyLocationEnabled(true);
        // ...
    }

}
于 2014-07-30T09:00:18.743 回答
0

尝试使用以下代码在地图中添加标记和折线:

GoogleMap mMap;
static final CameraPosition BONDI =
        new CameraPosition.Builder().target(new LatLng(-33.891614, 151.276417))
                .zoom(15.5f)
                .bearing(300)
                .tilt(50)
                .build();
changeCamera(CameraUpdateFactory.newCameraPosition(BONDI));
    mMap.addMarker(new MarkerOptions().position(new LatLng(-33.891614, 151.276417)).title("Bondi"));
 private void changeCamera(CameraUpdate update) {
    changeCamera(update, null);
    }
/**
 * Change the camera position by moving or animating the camera depending on the state of the
 * animate toggle button.
 */
private void changeCamera(CameraUpdate update, CancelableCallback callback) {
    boolean animated = ((CompoundButton) findViewById(R.id.animate)).isChecked();
    if (animated) {
        mMap.animateCamera(update, callback);
    } else {
        mMap.moveCamera(update);
    }
}

添加折线如下:

 // A simple polyline with the default options from Melbourne-Adelaide-Perth.
    mMap.addPolyline((new PolylineOptions())
            .add(BONDI));
于 2013-02-21T13:03:07.497 回答