如何将 Overlay 用于带有片段的 osmdroid

How to use Overlay for osmdroid with fragments

我已经设法 运行 使用片段在一个应用程序中实现了 OSM 和 GMS。而且我知道如何使用活动在应用程序中使用标记(覆盖)。我在带有片段的应用程序中为 OSM 使用 Overlay 时遇到问题,因为要使用 OSM,我需要为其创建静态 class 并且 ItemizedOverlay 需要对无法从静态上下文引用的 ResourceProxy 使用 getApplicationContext() 方法。

这是一个工作 OSM class 使用片段:

    public static class MapOSM extends Fragment{               // Klasa odpowiedzialna za fragment z mapą OSM

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        LinearLayout rl = (LinearLayout)inflater.inflate(R.layout.activity_osm, container, false);

        osmRl = (MapView) rl.findViewById(R.id.mapOsm2);
        osmRl.getTileProvider().setTileSource(TileSourceFactory.MAPNIK);

        return rl;
    }
}

我需要使用这个来让 Overlay 工作:

    ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
    myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
    map.getOverlays().add(myItemizedOverlay);

感谢您的回答:)

所以,感谢 "k3b",我正在使用片段发布我的工作 OSM class,添加了 2 个标记。

public static class MapOSM extends Fragment{               // Klasa odpowiedzialna za fragment z mapą OSM

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.activity_osm, container, false);

        osmRl = (MapView) ll.findViewById(R.id.mapOsm2);
        osmRl.getTileProvider().setTileSource(TileSourceFactory.MAPNIK);

        osmRl.setBuiltInZoomControls(true);
        osmRl.setMultiTouchControls(true);

        //Overlay
        MyItemizedOverlay myItemizedOverlay = null;

        IMapController mapController = osmRl.getController();
        mapController.setZoom(9);
        GeoPoint startPoint = new GeoPoint(54.792140, 18.402407);
        mapController.setCenter(startPoint);

        /// Markery na mapie
        Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
        assert marker != null;
        int markerWidth = marker.getIntrinsicWidth();
        int markerHeight = marker.getIntrinsicHeight();
        marker.setBounds(0, markerHeight, markerWidth, 0);

        ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getActivity().getApplicationContext());

        myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
        osmRl.getOverlays().add(myItemizedOverlay);

        GeoPoint myPoint1 = new GeoPoint(54.792140, 18.402407);
        myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
        GeoPoint myPoint2 = new GeoPoint(54.752, 18.422);
        myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");

        return ll;
    }
}

希望对大家有所帮助:)