Google使用标记群集映射样式

时间:2018-04-06 02:59:23

标签: android google-maps

我可以在android中添加带有标记簇的Map样式吗?因为我尝试使用这一行:

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    MapStyleOptions style = loadRawResourceStyle(Mapa.this, R.raw.estilo_map);
    mMap.setMapStyle(style);


    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-33.502482, -70.573841), 8));
    mMap.getUiSettings().setMapToolbarEnabled(false);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(false);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);
    mMap.setMinZoomPreference(10);
    mMap.setMaxZoomPreference(20);
    mMap.setLatLngBoundsForCameraTarget(new LatLngBounds(new LatLng(-33.660048, -70.801025),new LatLng(-33.271212, -70.471835)));


    mClusterManager = new ClusterManager<>(Mapa.this, mMap);
    RenderClusterInfoWindow renderer = new RenderClusterInfoWindow(Mapa.this, mMap, mClusterManager);
    mClusterManager.setRenderer(renderer);
    mMap.setOnCameraIdleListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);
    mMap.setOnInfoWindowClickListener(mClusterManager);
    clusterMarcadores();

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<FormatoMarcadores>() {
        @Override
        public boolean onClusterItemClick(FormatoMarcadores formatoMarcadores) {//abrir valorar y reportar aqui
            Log.d("ciclovia", formatoMarcadores.getTitle());
            d.ruta(formatoMarcadores.getTitle(), mMap);
            markerClick(formatoMarcadores.getTitle(),formatoMarcadores.getPosition());
            return false;
        }
    });

    mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<FormatoMarcadores>() {
        @Override
        public boolean onClusterClick(Cluster<FormatoMarcadores> cluster) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cluster.getPosition(), (float) Math.floor(mMap.getCameraPosition().zoom + 2)));
            return true;
        }
    });

    mClusterManager.cluster();

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            d.quitarPolyline();
            if (bottomSheet.isHideable()){
                bottomSheet.setState(BottomSheetBehavior.STATE_HIDDEN);
            }
        }
    });
}

我有这个:link to picture

地图在没有样式的情况下完美运行,如果我使用 mMap.setMapStyle(样式); 作为布尔值,我会正确应用样式

2 个答案:

答案 0 :(得分:1)

here

创建地图主题

下载你的style.json - &gt;复制原始资源文件

mGoogleMap.setMapStyle(
                        MapStyleOptions.loadRawResourceStyle(
                                this, R.raw.style_json));

答案 1 :(得分:0)

试试这段代码:

public class CustomMarkerClusteringDemoActivity extends BaseDemoActivity implements ClusterManager.OnClusterClickListener<Person>, ClusterManager.OnClusterInfoWindowClickListener<Person>, ClusterManager.OnClusterItemClickListener<Person>, ClusterManager.OnClusterItemInfoWindowClickListener<Person> {
private ClusterManager<Person> mClusterManager;
private Random mRandom = new Random(1984);

/**
 * Draws profile photos inside markers (using IconGenerator).
 * When there are multiple people in the cluster, draw multiple photos (using MultiDrawable).
 */
private class PersonRenderer extends DefaultClusterRenderer<Person> {
    private final IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
    private final IconGenerator mClusterIconGenerator = new IconGenerator(getApplicationContext());
    private final ImageView mImageView;
    private final ImageView mClusterImageView;
    private final int mDimension;

    public PersonRenderer() {
        super(getApplicationContext(), getMap(), mClusterManager);

        View multiProfile = getLayoutInflater().inflate(R.layout.multi_profile, null);
        mClusterIconGenerator.setContentView(multiProfile);
        mClusterImageView = (ImageView) multiProfile.findViewById(R.id.image);

        mImageView = new ImageView(getApplicationContext());
        mDimension = (int) getResources().getDimension(R.dimen.custom_profile_image);
        mImageView.setLayoutParams(new ViewGroup.LayoutParams(mDimension, mDimension));
        int padding = (int) getResources().getDimension(R.dimen.custom_profile_padding);
        mImageView.setPadding(padding, padding, padding, padding);
        mIconGenerator.setContentView(mImageView);
    }

    @Override
    protected void onBeforeClusterItemRendered(Person person, MarkerOptions markerOptions) {
        // Draw a single person.
        // Set the info window to show their name.
        mImageView.setImageResource(person.profilePhoto);
        Bitmap icon = mIconGenerator.makeIcon();
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).title(person.name);
    }

    @Override
    protected void onBeforeClusterRendered(Cluster<Person> cluster, MarkerOptions markerOptions) {
        // Draw multiple people.
        // Note: this method runs on the UI thread. Don't spend too much time in here (like in this example).
        List<Drawable> profilePhotos = new ArrayList<Drawable>(Math.min(4, cluster.getSize()));
        int width = mDimension;
        int height = mDimension;

        for (Person p : cluster.getItems()) {
            // Draw 4 at most.
            if (profilePhotos.size() == 4) break;
            Drawable drawable = getResources().getDrawable(p.profilePhoto);
            drawable.setBounds(0, 0, width, height);
            profilePhotos.add(drawable);
        }
        MultiDrawable multiDrawable = new MultiDrawable(profilePhotos);
        multiDrawable.setBounds(0, 0, width, height);

        mClusterImageView.setImageDrawable(multiDrawable);
        Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster cluster) {
        // Always render clusters.
        return cluster.getSize() > 1;
    }
}

@Override
public boolean onClusterClick(Cluster<Person> cluster) {
    // Show a toast with some info when the cluster is clicked.
    String firstName = cluster.getItems().iterator().next().name;
    Toast.makeText(this, cluster.getSize() + " (including " + firstName + ")", Toast.LENGTH_SHORT).show();

    // Zoom in the cluster. Need to create LatLngBounds and including all the cluster items
    // inside of bounds, then animate to center of the bounds.

    // Create the builder to collect all essential cluster items for the bounds.
    LatLngBounds.Builder builder = LatLngBounds.builder();
    for (ClusterItem item : cluster.getItems()) {
        builder.include(item.getPosition());
    }
    // Get the LatLngBounds
    final LatLngBounds bounds = builder.build();

    // Animate camera to the bounds
    try {
        getMap().animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

@Override
public void onClusterInfoWindowClick(Cluster<Person> cluster) {
    // Does nothing, but you could go to a list of the users.
}

@Override
public boolean onClusterItemClick(Person item) {
    // Does nothing, but you could go into the user's profile page, for example.
    return false;
}

@Override
public void onClusterItemInfoWindowClick(Person item) {
    // Does nothing, but you could go into the user's profile page, for example.
}

@Override
protected void startDemo() {
    getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 9.5f));

    mClusterManager = new ClusterManager<Person>(this, getMap());
    mClusterManager.setRenderer(new PersonRenderer());
    getMap().setOnCameraIdleListener(mClusterManager);
    getMap().setOnMarkerClickListener(mClusterManager);
    getMap().setOnInfoWindowClickListener(mClusterManager);
    mClusterManager.setOnClusterClickListener(this);
    mClusterManager.setOnClusterInfoWindowClickListener(this);
    mClusterManager.setOnClusterItemClickListener(this);
    mClusterManager.setOnClusterItemInfoWindowClickListener(this);

    addItems();
    mClusterManager.cluster();
}

private void addItems() {
    // http://www.flickr.com/photos/sdasmarchives/5036248203/
    mClusterManager.addItem(new Person(position(), "Walter", R.drawable.walter));

    // http://www.flickr.com/photos/usnationalarchives/4726917149/
    mClusterManager.addItem(new Person(position(), "Gran", R.drawable.gran));

    // http://www.flickr.com/photos/nypl/3111525394/
    mClusterManager.addItem(new Person(position(), "Ruth", R.drawable.ruth));

    // http://www.flickr.com/photos/smithsonian/2887433330/
    mClusterManager.addItem(new Person(position(), "Stefan", R.drawable.stefan));

    // http://www.flickr.com/photos/library_of_congress/2179915182/
    mClusterManager.addItem(new Person(position(), "Mechanic", R.drawable.mechanic));

    // http://www.flickr.com/photos/nationalmediamuseum/7893552556/
    mClusterManager.addItem(new Person(position(), "Yeats", R.drawable.yeats));

    // http://www.flickr.com/photos/sdasmarchives/5036231225/
    mClusterManager.addItem(new Person(position(), "John", R.drawable.john));

    // http://www.flickr.com/photos/anmm_thecommons/7694202096/
    mClusterManager.addItem(new Person(position(), "Trevor the Turtle", R.drawable.turtle));

    // http://www.flickr.com/photos/usnationalarchives/4726892651/
    mClusterManager.addItem(new Person(position(), "Teach", R.drawable.teacher));
}

private LatLng position() {
    return new LatLng(random(51.6723432, 51.38494009999999), random(0.148271, -0.3514683));
}

private double random(double min, double max) {
    return mRandom.nextDouble() * (max - min) + min;
}
}

请检查我的代码它是否正常工作我认为它可以帮助你。