I am creating an app where I am clustering multiple request on maps.
I am able to cluster request on map but I want to get click event on cluster. I tried using setOnClusterItemClickListener but I am unable to capture click event in that method.I am also having OnCameraChangeListener in map which runs when I tap on cluster.
Is this causing issue because of both listeners or I am doing something wrong.
Here's is my listener's code
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
cameraChangeHandler.removeCallbacks(throttledRunnable);
cameraChangeHandler.postDelayed(throttledRunnable, 300);
if (marker != null) {
marker.remove();
mClusterManager.clearItems();
}
LatLng latLng = cameraPosition.target;
appSharedPreference.setLatitude(String.valueOf(latLng.latitude));
appSharedPreference.setLongitude(String.valueOf(latLng.longitude));
marker = map.addMarker(new MarkerOptions().position(latLng));
submitData();
}
});
map.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem myItem) {
Toast.makeText(getActivity(),"Hello",Toast.LENGTH_LONG).show();
return false;
}
});
Using the link provided I was able to get this working using following code:
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
if (marker != null) {
marker.remove();
mClusterManager.clearItems();
}
LatLng latLng = cameraPosition.target;
appSharedPreference.setLatitude(String.valueOf(latLng.latitude));
appSharedPreference.setLongitude(String.valueOf(latLng.longitude));
// marker = map.addMarker(new MarkerOptions().position(latLng));
submitData();
pDialouge.hide();
}
});
mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), map, mClusterManager));
map.setOnMarkerClickListener(mClusterManager);
mClusterManager
.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
#Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
clickedCluster = cluster;
return false;
}
});
mClusterManager
.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem item) {
clickedClusterItem = item;
return false;
}
});
Related
I am using GoogleMaps marker clustering available in the utils library. On clicking a Cluster, the below onClusterClick() and onClusterItemClick() method is not called. Is there a Cluster click event?
#Override
public void onMapReady(GoogleMap googleMap) {
/*start clustring*/
clusterManager = new ClusterManager<>(getActivity(), map);
map.setOnCameraIdleListener(clusterManager);
/*end clustring*/
LatLng karawanbazar1 = new LatLng(36.861666, 10.156551);
LatLng karawanbazar2 = new LatLng(36.860179, 10.152874);
MyItemMap myItemMap1 = new MyItemMap(karawanbazar1);
MyItemMap myItemMap2 = new MyItemMap(karawanbazar2);
clusterManager.addItem(myItemMap1);
clusterManager.addItem(myItemMap2);
clusterManager.cluster();
clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItemMap>() {
#Override
public boolean onClusterItemClick(MyItemMap myItemMap) {
Toast.makeText(getContext(),"onclusterItemClick"+ myItemMap.getPosition(),Toast.LENGTH_LONG);
System.out.println("this is cluster clusteItemClick "+myItemMap.toString() );
return true;
}
});
clusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItemMap>() {
#Override
public boolean onClusterClick(Cluster<MyItemMap> cluster) {
Toast.makeText(getContext(),"setOnClusterClickListener"+ cluster.getSize(),Toast.LENGTH_LONG);
System.out.println("this is cluster onClusterClick "+cluster.toString() );
return true;
}
});
}
Propably what you are looking for is a method setOnMarkerClickListener() of GoogleMap object:
googleMap.setOnMarkerClickListener(clusterManager)
Then you have to set:
clusterManager.setOnClusterClickListener()
All i had to do is to add this line before using events on clusters.
map.setOnMarkerClickListener(yourClusterManager);
I'm having trouble putting my current/updated location on the map which has other data. please see the image below. I need it because I will do a distance between the vehicle location and my current location, anyways how to do it? this activity is extended by fragment. I really need your help.
This is my OnMapReady:
public void onMapReady(GoogleMap googleMap) {
mMapProximity = googleMap;
mMapProximity.setBuildingsEnabled(true);
mMapProximity.getUiSettings().setRotateGesturesEnabled(false);
setUpMap();
}
private void setUpMap() {
mMapProximity.clear();
mMapProximity.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMapProximity.setIndoorEnabled(true);
mMapProximity.setBuildingsEnabled(true);
mMapProximity.getUiSettings().setZoomControlsEnabled(false);
final Double lati = Double.parseDouble(latitudeProxListMap);
final Double longi = Double.parseDouble(longitudeProxListMap);
m = mMapProximity.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_logo))
.visible(true));
mMapProximity.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker.showInfoWindow();
}
});
mMapProximity.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lati, longi), 17.0f));
mMapProximity.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
return true;
}
});
mMapProximity.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
m.showInfoWindow();
}
class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(R.layout.info_vehicle_list_map, null);
TextView title =v.findViewById(R.id.title);
TextView snippet =v.findViewById(R.id.snippet);
snippet.setVisibility(View.GONE);
title.setText("Location: " + location);
return v;
}
}
Implement LocationListener in your activity and than in onLocationChange method update the marker with the current location values. That will put a marker on the current location.
I'm trying to implement clusters for my googlemaps markers. Currently I have some custom marker colors with an API call that fires when you click on them. When those results are loaded, there will open a bottomsheet with the specific information of that marker.
I want to keep the same functionalities (custom markers/clickListeners/radius around markers), but add clusters when zoomed out. I've looked at different sources for help:
Android cluster and marker clicks
Android marker-clustering
MarkerManager.java
But i'm not sure how to implement the custom marker and listener for the clusters items. I'am able to get clusters with standard markers without clicklisteners. Here are some images for illustration:
This is my current situation (I want to cluster these markers). As you can see, the bottom sheet pops up when I click on a marker
This is what I'm currently able to do, but I want to combine it with the previous picture
Here is the important part of my code of my map Fragment, (The Point class does implement the ClusterItem interface):
private Map<Marker, Point> retailerInfo = new HashMap<>();
private void markGeofencesOnMap() {
new GeofenceAreasRequest().getAllAreas(new GeofenceAreasCallback() {
#Override
public void onAreasLoaded(List<Point> points) {
for (final Point point : points) {
markerForGeofence(point);
drawRadius(point);
}
}
#Override
public void failedOnAreasLoaded(int message) {
Snackbar.make(coordinatorLayout, R.string.failed_loading_areas, Snackbar.LENGTH_LONG).show();
}
});
}
private void markerForGeofence(Point point) {
LatLng latLng = new LatLng(point.getLatitude(), point.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.flat(true)
.title(point.getTitle())
.icon(BitmapDescriptorFactory.defaultMarker(195));
// markerManager.getCollection("markerCollection").addMarker(markerOptions);
// markerManager.getCollection("markerCollection").setOnMarkerClickListener(this);
// mClusterManager.addItem(point);
geoFenceMarker = googleMap.addMarker(markerOptions);
retailerInfo.put(geoFenceMarker, point);
}
private void drawRadius(Point point) {
CircleOptions circleOptions = new CircleOptions()
.center(geoFenceMarker.getPosition())
.strokeColor(Color.argb(50, 70, 70, 70))
.fillColor(Color.argb(100, 150, 150, 150))
.radius(point.getRadius());
googleMap.addCircle(circleOptions);
}
private void googleMapSettings() {
int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
} else {
SystemRequirementsChecker.checkWithDefaultDialogs(getActivity());
}
googleMap.getUiSettings().setZoomControlsEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder().target(getLastLocation()).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.setOnMapClickListener(this);
// markerManager = new MarkerManager(googleMap);
// markerManager.newCollection("markerCollection");
// mClusterManager = new ClusterManager<Point>(getActivity(), googleMap );//, markerManager);
// googleMap.setOnMarkerClickListener(mClusterManager); //markerManager);
googleMap.setOnCameraIdleListener(mClusterManager);
googleMap.setOnMarkerClickListener(this);
}
#Override
public boolean onMarkerClick(Marker marker) {
if (retailerInfo != null) {
String retailerId = retailerInfo.get(marker).getRetailer();
new RetailersRequest().getRetailer(retailerId, new RetailersCallback() {
#Override
public void onRetailersLoad(List<Retailer> retailers) {
for (final Retailer retailer : retailers) {
mMapRetailerName.setText(retailer.getName());
mMapRetailerStreet.setText(retailer.getStreet());
mMapRetailerHouseNr.setText(retailer.getHousenumber());
mMapRetailerPostalCode.setText(retailer.getPostalCode());
mMapRetailerCity.setText(retailer.getCity());
}
bottomSheet.setVisibility(View.VISIBLE);
mBottomSheetBehavior.setPeekHeight(400);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
#Override
public void failedOnRetailersLoaded(int code) {
Snackbar.make(coordinatorLayout, getString(R.string.failed_loading_retailers) + code, Snackbar.LENGTH_LONG).show();
}
});
CameraPosition cameraPosition = new CameraPosition.Builder().target(marker.getPosition()).zoom(14).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
return true;
}
#Override
public void onMapClick(LatLng latLng) {
bottomSheet.setVisibility(View.GONE);
}
#Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
googleMapSettings();
markGeofencesOnMap();
}
I hope someone can help me out in the right direction. Thx!
Okay, after some more trail and error, I think I've got it sorted out for the most part. All I had to do was to attach a custom DefaultClusterRenderer to my clusterManager. In the mapfragment I could use onClusterItemClick for handling the marker clicks. The one problem I still have is that the circles are not all properly rendered when zooming in and out.
private void markGeofencesOnMap() {
new GeofenceAreasRequest().getAllAreas(new GeofenceAreasCallback() {
#Override
public void onAreasLoaded(List<Point> points) {
for (final Point point : points) {
mClusterManager.addItem(point);
}
}
});
}
private void googleMapSettings() {
mClusterManager = new ClusterManager<Point>(getActivity(), googleMap );
// attach custom renderer behaviour
mClusterManager.setRenderer(new OwnPointRendered(getActivity().getApplicationContext(), googleMap, mClusterManager));
mClusterManager.setOnClusterItemClickListener(this);
googleMap.setOnMarkerClickListener(mClusterManager);
googleMap.setOnCameraIdleListener(mClusterManager);
}
#Override
public boolean onClusterItemClick(ClusterItem clusterItem) {
// cast ClusterItem to my Point class to handle marker clicks
Point retailer = (Point) clusterItem;
String retailerId = retailer.getRetailer();
return true;
}
OwnPointRendered.class
public class OwnPointRendered extends DefaultClusterRenderer<Point> {
private final GoogleMap map;
private List<Circle> circleList = new ArrayList<>();
public OwnPointRendered(Context context, GoogleMap map,
ClusterManager<Point> clusterManager) {
super(context, map, clusterManager);
this.map = map;
}
#Override
protected void onBeforeClusterItemRendered(Point item, MarkerOptions markerOptions) {
markerOptions.flat(true);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(195));
drawRadius(item);
super.onBeforeClusterItemRendered(item, markerOptions);
}
#Override
protected void onClusterRendered(Cluster<Point> cluster, Marker marker) {
super.onClusterRendered(cluster, marker);
for (Circle circle : circleList) {
circle.remove();
}
circleList.clear();
}
private void drawRadius(Point point) {
CircleOptions circleOptions = new CircleOptions()
.center(point.getPosition())
.strokeColor(Color.argb(50, 70, 70, 70))
.fillColor(Color.argb(100, 150, 150, 150))
.radius(point.getRadius());
Circle circle = map.addCircle(circleOptions);
circleList.add(circle);
}
I have been trying for hours to add a new marker onLongMapClick in the code below. I've searched a lot but nothing seems to be working. I'm using Android Studio 1.5.1 and Google Maps API. I'm testing my app on a Nexus 6P emulator. API 23. I left it blank between the brackets at the bottom where I think the code is supposed to go. The code below is in public class BasicMapDemoActivity extends AppCompatActivity implements OnMapReadyCallback { and I can't seem to figure out how to do it. Can anyone please help me make this happen? I can provide what ever other information might be useful. Thank you!
GoogleMap mMap;
private UiSettings mUiSettings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if (item.getItemId() == R.id.menu_legal) {
startActivity(new Intent(this, LegalInfoActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
mUiSettings = mMap.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
mUiSettings.setScrollGesturesEnabled(true);
mUiSettings.setZoomGesturesEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
}
});
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng point) {
}
});
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
float maxZoom = 7.0f;
if (position.zoom > maxZoom) {
mMap.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
}
}
});
}
}
As you already have the reference of mMap, so try this code
(Note: I haven't tried it.)
LatLng latlng= new LatLng(LATITUDE,LONGITUDE);
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 13));
mMap.addMarker(new MarkerOptions()
.title("NAME THE MARKER POINT")
.snippet("DESCRIPTION FOR MARKER HERE.")
.position(latlng));
you should add a marker in the onMapLongClick brackets like this
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng point) {
MarkerOptions options = new MarkerOptions()
.position(point);
Marker mMarker = mMap.addMarker(options);
}
});
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;
}
});