Skip to content
Permalink
Browse files

Custom Marker added for Marker Clustering...

  • Loading branch information...
AhsenSaeed committed May 9, 2019
1 parent b44fb04 commit bb54e16bcf19ba03d7d1aa1be0cac45e7a48010d
@@ -5,7 +5,7 @@
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
@@ -16,6 +16,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility = '1.8'
sourceCompatibility = '1.8'
}
}

dependencies {
@@ -4,19 +4,24 @@
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.maps.android.clustering.Cluster;
import com.google.maps.android.clustering.ClusterManager;

import java.util.Arrays;
import java.util.List;

import spartons.com.googlemapcustommarkercluster.clusterRenderer.MarkerClusterRenderer;
import spartons.com.googlemapcustommarkercluster.data.User;
import spartons.com.googlemapcustommarkercluster.util.GoogleMapHelper;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements ClusterManager.OnClusterClickListener<User> {

private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -28,17 +33,16 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
assert supportMapFragment != null;
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMapHelper.defaultMapSettings(googleMap);
setUpClusterManager(googleMap);
}
supportMapFragment.getMapAsync(googleMap -> {
this.googleMap = googleMap;
GoogleMapHelper.defaultMapSettings(googleMap);
setUpClusterManager(googleMap);
});
}

private void setUpClusterManager(GoogleMap googleMap) {
ClusterManager<User> clusterManager = new ClusterManager<>(this, googleMap);
clusterManager.setRenderer(new MarkerClusterRenderer(this, googleMap, clusterManager));
googleMap.setOnCameraIdleListener(clusterManager);
List<User> items = getItems();
clusterManager.addItems(items);
@@ -72,4 +76,19 @@ private void setUpClusterManager(GoogleMap googleMap) {
new User("CodingInfinite23", new LatLng(-43.999792, 170.463352))
);
}

@Override
public boolean onClusterClick(Cluster<User> cluster) {
if (cluster == null) return false;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (User user : cluster.getItems())
builder.include(user.getPosition());
LatLngBounds bounds = builder.build();
try {
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
@@ -0,0 +1,41 @@
package spartons.com.googlemapcustommarkercluster.clusterRenderer;


import android.content.Context;
import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;
import com.google.maps.android.ui.IconGenerator;

import spartons.com.googlemapcustommarkercluster.R;
import spartons.com.googlemapcustommarkercluster.data.User;

public class MarkerClusterRenderer extends DefaultClusterRenderer<User> {

private static final int MARKER_DIMENSION = 48;

private final IconGenerator iconGenerator;
private final ImageView markerImageView;

public MarkerClusterRenderer(Context context, GoogleMap map, ClusterManager<User> clusterManager) {
super(context, map, clusterManager);
iconGenerator = new IconGenerator(context);
markerImageView = new ImageView(context);
markerImageView.setLayoutParams(new ViewGroup.LayoutParams(MARKER_DIMENSION, MARKER_DIMENSION));
iconGenerator.setContentView(markerImageView);
}

@Override
protected void onBeforeClusterItemRendered(User item, MarkerOptions markerOptions) {
markerImageView.setImageResource(R.drawable.location_vector_icon);
Bitmap icon = iconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
markerOptions.title(item.getTitle());
}
}
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:tint="#5A409E"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C8.14,2 5,5.14 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.86 -3.14,-7 -7,-7zM12,4c1.1,0 2,0.9 2,2 0,1.11 -0.9,2 -2,2s-2,-0.89 -2,-2c0,-1.1 0.9,-2 2,-2zM12,14c-1.67,0 -3.14,-0.85 -4,-2.15 0.02,-1.32 2.67,-2.05 4,-2.05s3.98,0.73 4,2.05c-0.86,1.3 -2.33,2.15 -4,2.15z" />
</vector>
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="google_maps_api_key">AIzaSyDLJa************************xtvNMRGNuxk</string>
// replace the your google api key.
<string name="google_maps_api_key">AIzaSyDLJaKBtmH2CGHV8tJfHV_HxtvNMRGNuxk</string>
</resources>

0 comments on commit bb54e16

Please sign in to comment.
You can’t perform that action at this time.