android googlemap marker的图标没有改变

时间:2016-12-07 16:11:33

标签: android google-maps google-maps-api-3

我有一个Viewpager,它有4个标签,其中一个标签有一个地图。 我将在运行时更改Android GoogleMap标记的图标。我做了以下代码

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


        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;

                // For showing a move to my location button
                googleMap.setMyLocationEnabled(true);

                // For dropping a marker at a point on the Map

                /* googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description")).
                        setIcon(BitmapDescriptorFactory.fromResource(R.drawable.position_icon));*/
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

    @Override
    public void onMyLocationChange(Location location) {

        sydney=new LatLng(location.getLatitude(), location.getLongitude());
        googleMap.addMarker(new MarkerOptions().position(sydney).title(""))
        .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.position_icon));
        CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

       googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.logo));
                return false;
            }
        });
    }


});

                // For zooming automatically to the location of the marker
               /* */
            }
        });
        return rootView;
    }

但不幸的是,我点击了标记图标,它已经改变,但在几秒之后,前一个标记与新标记图标重叠。 上面的代码是TabLayout的第一个标签,我说有一个地图,它是一个片段。

1 个答案:

答案 0 :(得分:0)

上述问题的问题是图标会在onClickItem事件中更改后撤消。所以我定义了一个类全局属性名称marker_change,然后用简单的if语句控制。

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


        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately
        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;

                // For showing a move to my location button
                googleMap.setMyLocationEnabled(true);

                // For dropping a marker at a point on the Map

                /* googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description")).
                        setIcon(BitmapDescriptorFactory.fromResource(R.drawable.position_icon));*/
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

    @Override
    public void onMyLocationChange(Location location) {

        sydney=new LatLng(location.getLatitude(), location.getLongitude());
        googleMap.addMarker(new MarkerOptions().position(sydney))
        .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.position_icon));

        CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(final Marker marker) {

marker_change=1;
                return false;
            }
        });

               if(marker_change==1) {

                   googleMap.clear();
                   MarkerOptions mo=new MarkerOptions().position(sydney).title("please take pic").snippet("apple")
                           .icon(BitmapDescriptorFactory.fromResource(R.drawable.address_icon));

//            googleMap.addMarker(mo).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_new_mark_icon));;
                   googleMap.addMarker(mo).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_new_mark_icon));

                   cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                   googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
               } else {
                   marker_change=0;
               }

    }


});


            }
        });
        return rootView;
    }