如何更改片段上的地图视图?

时间:2016-04-25 19:47:31

标签: android mapbox

我正在使用mapbox api开发应用。在地图上,当用户点击菜单上的地图项目时,片段正在打开。后期用户可能想要更改地图视图样式,例如卫星,黑暗,祖母绿或仅街景。用户可以在操作栏上选择地图样式。我在主要活动上实施了我的操作栏。当地图片段上的用户选择地图样式并且变量到达主要活动中处理的片段时。此变量是地图样式的字符串。现在我想更改地图视图样式而不重新创建地图片段。你能对这个问题提出什么建议?

public class GeoMapFragment extends Fragment implements OnMapReadyCallback {

    public MapView mapView = null;
    private MapboxMap mapboxMap;
    public String mapViewSelectedItem ;

    private static final int PERMISSIONS_LOCATION = 0;

    public GeoMapFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_geomap, container, false);


        mapView = (MapView)view.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this);

        // Inflate the layout for this fragment
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * Dispatch onPause() to fragments.
     */
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }


    @Override
    public void onMapReady(MapboxMap map) {

        mapboxMap = map;
        mapboxMap.setStyle(Style.DARK);
        mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(0, 0)));

        // Show user location (purposely not in follow mode)
        if ((ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
                (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_LOCATION);
        } else {
            mapboxMap.setMyLocationEnabled(true);
        }

        // TODO - mv.loadFromGeoJSONURL("https://gist.githubusercontent.com/tmcw/10307131/raw/21c0a20312a2833afeee3b46028c3ed0e9756d4c/map.geojson");
        mapboxMap.addMarker(new MarkerOptions().title("Edinburgh").snippet("Scotland").position(new LatLng(55.94629, -3.20777)));
        mapboxMap.addMarker(new MarkerOptions().title("Stockholm").snippet("Sweden").position(new LatLng(59.32995, 18.06461)));
        mapboxMap.addMarker(new MarkerOptions().title("Prague").snippet("Czech Republic").position(new LatLng(50.08734, 14.42112)));
        mapboxMap.addMarker(new MarkerOptions().title("Athens").snippet("Greece").position(new LatLng(37.97885, 23.71399)));
        mapboxMap.addMarker(new MarkerOptions().title("Tokyo").snippet("Japan").position(new LatLng(35.70247, 139.71588)));
        mapboxMap.addMarker(new MarkerOptions().title("Ayacucho").snippet("Peru").position(new LatLng(-13.16658, -74.21608)));
        mapboxMap.addMarker(new MarkerOptions().title("Nairobi").snippet("Kenya").position(new LatLng(-1.26676, 36.83372)));
        mapboxMap.addMarker(new MarkerOptions().title("Canberra").snippet("Australia").position(new LatLng(-35.30952, 149.12430)));

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMISSIONS_LOCATION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mapboxMap.setMyLocationEnabled(true);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您的地图片段应该有一个getMapAsync()调用,您可以更改其样式。下面是一个如何实现这一目标的粗略例子:

private MapboxMap map;

...

mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final MapboxMap mapboxMap) {

            map = mapboxMap;

        }
    });

...

// Wherever you implemented receiving the style string from MainActivity you can include this:

map.setStyleUrl(<style url>);

如果这对您不起作用,请告诉我您是如何创建地图片段的。目前,SDK包含一个片段,你可以这样设置:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment mapFragment;
    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        LatLng patagonia = new LatLng(-52.6885, -70.1395);

        MapboxMapOptions options = new MapboxMapOptions();
        options.accessToken("<your access token here>");
        options.styleUrl(Style.SATELLITE);
        options.camera(new CameraPosition.Builder()
                .target(patagonia)
                .zoom(9)
                .build());

        mapFragment = SupportMapFragment.newInstance(options);

        transaction.add(R.id.container, mapFragment, "mapFragment");
        transaction.commit();
    } else {
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("mapFragment");
    }

    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final MapboxMap mapboxMap) {

            map = mapboxMap;

        }
    });
}

如果你这样做,你可以像我上面说的那样轻松改变地图的风格。

希望这会有所帮助!