How to determine what cluster item is clicked? - android

I am using Android Maps Utils. I am reading a list of coordinates from online and plotting them as cluster items as well as saving them in a hash map which associates a "Room" a class I have created to the cluster item:
private HashMap roomHashMap = new HashMap();
On clicking the info window of this cluster item I need to retrieve the room associated with the cluster item. I had implemented this using a marker with no problem as in the onInfoWindowClickListener I just added roomHashMap.get(marker) but now I cannot do this because in the info window it still requires a marker but I have a HashMap of ClusterItem
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
//Ideally I want this
//Room currenRoom=roomHashMap.get(clusterItem);
//but clusterItem is obviously not a Marker
}
});

googleMap = mFragment.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.getUiSettings().setZoomControlsEnabled(true); // true to
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
if (googleMap == null) {
Toast.makeText(getActivity(), "Sorry! unable to create maps",
Toast.LENGTH_SHORT).show();
}
mClusterManager = new ClusterManager<MyItem>(getActivity(), googleMap );
googleMap.setOnMapLoadedCallback(this);
googleMap.setMyLocationEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
markers = new Hashtable<String, String>();
mClusterManager.setRenderer(new MyClusterRenderer(getActivity() , googleMap , mClusterManager ));
public class MyClusterRenderer extends DefaultClusterRenderer {
public MyClusterRenderer(Context context, GoogleMap map,
ClusterManager<MyItem> clusterManager) {
super(context, map, clusterManager);
}
#Override
protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
super.onBeforeClusterItemRendered(item, markerOptions);
markerOptions.title(item.getTitle());
markerOptions.snippet(item.getAddress());
}
#Override
protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
super.onClusterItemRendered(clusterItem, marker);
//here you have access to the marker itself
}
}

Related

Add multiple cluster marker in a map android studio

I follow tutorial cluster google map and add in my project, it run.
But now I have two type item in a map so I want create two cluster manager for each type item.
I search and see multiple cluster manager but when I can't add onCameraChange in setOnCameraChangeListener() method.
How I can add multiple cluster in a map?
Thank you very much!
#Override
public void onMapReady(GoogleMap googleMap) {
isMapReady = true;
map = googleMap;
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.getUiSettings().setCompassEnabled(false);
Gps lastGps = ((PagerActivity) getActivity()).getLastGPS();
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(lastGps.getLat(), lastGps.getLng()))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.radar_boy));
map.addMarker(markerOptions);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastGps.getLat(), lastGps.getLng()), 12));
clusterManager = new ClusterManager<MarkerItem>(getContext(), map);
clusterManager1 = new ClusterManager<MarkerItem>(getContext(), map);
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
clusterManager.onCameraIdle();
clusterManager1.onCameraIdle();
}
});
ClusterRenderer clusterRenderer = new ClusterRenderer(getContext(), map, clusterManager);
clusterManager.setRenderer(clusterRenderer);
ClusterRenderer clusterRenderer1 = new ClusterRenderer(getContext(), map, clusterManager1);
clusterManager1.setRenderer(clusterRenderer1);
addMarker();
}
private void addMarker() {
List<MyItem> itemsGold = new ArrayList<>();// list item type 1
List<MyItem> itemsGoldOre = new ArrayList<>();// list item type 2
....// add item in two list
// add item in map
if (isLoadDataComplete && isMapReady) {
for (Point point : listGold) {
DetailItemApi.DetailItem detailItem = detailItemUtil.getDetailItem(point.type);
Glide.with(getActivity())
.load(baseUrl + detailItem.img.getRadar_s())
.asBitmap()
.into(new SimpleTarget<Bitmap>(100, 100) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(point.gps.getLat(), point.gps.getLng()))
.icon(BitmapDescriptorFactory.fromBitmap(resource));
MarkerItem markerItem = new MarkerItem(markerOptions);
clusterManager.addItem(markerItem);
}
});
}
for (Point point : listGoldOre) {
DetailItemApi.DetailItem detailItem = detailItemUtil.getDetailItem(point.type);
Glide.with(getActivity())
.load(baseUrl + detailItem.img.getRadar_s())
.asBitmap()
.into(new SimpleTarget<Bitmap>(100, 100) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(point.gps.getLat(), point.gps.getLng()))
.icon(BitmapDescriptorFactory.fromBitmap(resource));
MarkerItem markerItem = new MarkerItem(markerOptions);
clusterManager1.addItem(markerItem);
}
});
}
}
}
Use MarkerManager
#Override
public void onMapReady(GoogleMap googleMap) {
isMapReady = true;
map = googleMap;
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.getUiSettings().setCompassEnabled(false);
Gps lastGps = ((PagerActivity) getActivity()).getLastGPS();
MarkerOptions markerOptions = new MarkerOptions()
.position(new LatLng(lastGps.getLat(), lastGps.getLng()))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.radar_boy));
map.addMarker(markerOptions);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lastGps.getLat(), lastGps.getLng()), 12));
MarkerManager markerManager = new MarkerManager(map);
clusterManager = new ClusterManager<MarkerItem>(getContext(), map,
markerManager);
clusterManager1 = new ClusterManager<MarkerItem>(getContext(), map, markerManager);
map.setOnMarkerClickListener(markerManager);
map.setOnInfoWindowClickListener(markerManager);
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
clusterManager.onCameraIdle();
clusterManager1.onCameraIdle();
}
});
ClusterRenderer clusterRenderer = new ClusterRenderer(getContext(), map, clusterManager);
clusterManager.setRenderer(clusterRenderer);
ClusterRenderer clusterRenderer1 = new ClusterRenderer(getContext(), map, clusterManager1);
clusterManager1.setRenderer(clusterRenderer1);
addMarker();
clusterManager.cluster();
clusterManager1.cluster();
}

MarkerOptions .tag not available

I have a DefaultClusterRenderer class and my own ClusterItem, which I am using as marker on the map. I am setting the title and snippet with MarkerOptions, so I can grap them on my onInfoWindowClick event. I wanna start a new Activity, depends on which infoWindow the user clicked, but I cant add a tag to MarkerOptions, so I cant get the tag in my onInfoWindowClick method via the Marker parameter.
I am looking for a good solution to add an id to each ClusterItem and get the id via Marker in onInfoWindowClick.
public class OwnClusterRendering extends DefaultClusterRenderer<GeoPoint> {
private GoogleMap map;
public OwnClusterRendering(Context context, GoogleMap map, ClusterManager<GeoPoint> clusterManager) {
super(context, map, clusterManager);
this.map = map;
}
protected void onBeforeClusterItemRendered(GeoPoint geoPoint, MarkerOptions markerOptions) {
markerOptions.icon(geoPoint.getIcon());
markerOptions.snippet(geoPoint.getSnippet());
markerOptions.title(geoPoint.getTitle());
super.onBeforeClusterItemRendered(geoPoint, markerOptions);
}
}
MyCustomWindowAdapter:
#Override
public void onInfoWindowClick(Marker marker) {
Log.d("test", marker.getTitle());
Log.d("test", marker.getSnippet());
Log.d("test", marker.getId());
Log.d("test", String.valueOf(marker.getTag()));
// Intent intent = new Intent(context, StoneInfoSliderActivity.class);
// context.startActivity(intent);
}
Solved it with the following method from DefaultClusterRenderer<>
#Override
protected void onClusterItemRendered(GeoPoint geoPoint, Marker marker) {
marker.setTag(geoPoint.getId());
super.onClusterItemRendered(geoPoint, marker);
}

Add marker on long press in Google Maps API v3

How to implement adding a marker on long press in Google Maps API v3 for android? There are answers about this in Stack Overflow itself but they are for Google Maps API v2.
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advertiser_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
//for searching for a location
public void onMapSearch(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText);
String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
As #tyczj pointed out, there's no v3 for Android Google Maps API, so you are probably using v2.
That said, to accomplish what you want, call setOnMapLongClickListener in your mMap object, and add the marker as you want inside onMapLongClick. You should do this in the onMapReady method:
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
}
});
}
// EDIT:
If you want to keep only one marker present at a time, you should declare your marker in the global activity's scope, and then, in onMapLongClick, if marker already exists, instead of creating a new marker, just update it's position:
public class advertiserMap extends FragmentActivity implements OnMapReadyCallback {
// Declare marker globally
Marker myMarker;
...
#Override
public void onMapReady(GoogleMap googleMap) {
...
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
// First check if myMarker is null
if (myMarker == null) {
// Marker was not set yet. Add marker:
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Your marker title")
.snippet("Your marker snippet"));
} else {
// Marker already exists, just update it's position
myMarker.setPosition(latLng);
}
}
});
}
}
If done like this, remember to always check if your marker is not NULL before manipulating it in your code.
Hey guys if you all are using mMap.clear() then remove it . Mostly it work just fine.

Marker in google maps for android

I want to know how i can set "OnClick" for a marker on google maps in android.
Example: User touch the marker and My app detect this.
My marker
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(-18.142, 178.431), 2));
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMyLocationEnabled(true);
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
Try with OnMarkerClickListener, like following:
map.setOnMarkerClickListener(new OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker arg0) {
// do your stuff
return true;
}});
implement OnMarkerClickListener
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
myMarker = googleMap.addMarker();
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Example
this.googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
Log.d(TAG, arg0.getTitle() + " | " + arg0.getPosition().toString());
return false;
}
});

Touch listener on Google Map Markers Android

How can I add a Tab/Touch/Click listener on the Markers I have on my Google Map in android program. For example in the below image I have one marker on google maps and when clicked, I want to bring up a toast saying this has been clicked
http://i.stack.imgur.com/Bc7Lp.png
I have tried onMapClickListener but that does not work.
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
}
}
All markers in Google Android Maps Api v2 are clickable. You don't need to set any additional properties to your marker. What you need to do - is to register marker click callback to your googleMap and handle click within callback:
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener{
private Marker myMarker;
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Use this
map.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0)
Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
return true;
}
});
This will help to you :)

Categories

Resources