Show markers near the device in Android - android

I'm developing an android app which is having several kinds of markers.
Here is my MapsActivity.java file.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
GoogleMap googleMap;
List<MapLocation> restaurantList;
List<MapLocation> hotelList;
List<Marker> restaurantMarkers = new ArrayList<>();
List<Marker> hotelMarkers = new ArrayList<>();
MapLocation r1 = new MapLocation(6.9192,79.8950, "Mnhatten Fish Market" );
MapLocation r2 = new MapLocation(6.9017,79.9192, "Dinemore" );
MapLocation r3 = new MapLocation(6.9147,79.8778, "KFC" );
MapLocation r4 = new MapLocation(6.9036,79.9547, "McDonalds" );
MapLocation r5 = new MapLocation(6.8397,79.8758, "Dominos" );
MapLocation h1 = new MapLocation(6.9006,79.8533, "Hilton" );
MapLocation h2 = new MapLocation(6.8889,79.8567, "Galadari" );
MapLocation h3 = new MapLocation(6.8756,79.8608, "Hotel Lagoon Dining" );
MapLocation h4 = new MapLocation(6.7991,79.8767, "Aqua Pearl Lake Resort " );
MapLocation h5 = new MapLocation(6.5833,79.1667, "KZ Resort" );
//Buttons
private final LatLng LOCATION_COLOMBO = new LatLng(6.9270786,79.861243);
private final LatLng LOCATION_GALLE = new LatLng(6.0334009,80.218384);
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
restaurantList = new ArrayList<>();
hotelList = new ArrayList<>();
restaurantList.add(r1);
restaurantList.add(r2);
restaurantList.add(r3);
restaurantList.add(r4);
restaurantList.add(r5);
hotelList.add(h1);
hotelList.add(h2);
hotelList.add(h3);
hotelList.add(h4);
hotelList.add(h5);
// Give text to buttons
Button buttonloc1 = (Button)findViewById(R.id.btnLoc1);
buttonloc1.setText("Colombo");
Button buttonloc2 = (Button)findViewById(R.id.btnLoc2);
buttonloc2.setText("Galle");
Button buttoncity = (Button)findViewById(R.id.btnCity);
buttoncity.setText("My Location");
Button buttonremove = (Button) findViewById(R.id.removeMarker);
buttonremove.setText("Remove");
CheckBox checkRestaurants = (CheckBox) findViewById(R.id.checkRestaurants);
checkRestaurants.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showRestaurants();
} else {
hideRestaurants();
}
}
});
CheckBox checkHotels = (CheckBox) findViewById(R.id.checkHotels);
checkHotels.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
showHotels();
} else {
hideHotels();
}
}
});
// Marker to Dinemore
mMap.addMarker(new MarkerOptions()
.position(LOCATION_COLOMBO)
.title("I'm in Colombo :D")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
);
// Marker to Barista
mMap.addMarker(new MarkerOptions()
.position(LOCATION_GALLE)
.title("I'm in Galle :D")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
);
//When touch again on the map marker title will hide
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
}
});
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMyLocationChange(Location location) {
Location target = new Location("target");
for(LatLng point : new LatLng[]{}) {
target.setLatitude(point.latitude);
target.setLongitude(point.longitude);
if(location.distanceTo(target) < 100) {
// bingo!
}
}
}
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
setUpMap();
}
public void showRestaurants() {
restaurantMarkers.clear();
for (MapLocation loc : restaurantList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
restaurantMarkers.add(marker);
}
}
public void showHotels() {
hotelMarkers.clear();
for (MapLocation loc : hotelList){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(loc.lat, loc.lon))
.title(loc.title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loc.lat, loc.lon)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
hotelMarkers.add(marker);
}
}
public void hideRestaurants(){
for (Marker marker : restaurantMarkers){
marker.remove();
}
}
public void hideHotels(){
for (Marker marker : hotelMarkers){
marker.remove();
}
}
public void onClick_City(View v){
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
// When click on this button, Map shows the place of Dinemore
public void onClick_Loc1(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_COLOMBO,10);
mMap.animateCamera(update);
}
// When click on this button, Map shows the place of Barista
public void onClick_Loc2(View v) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_GALLE,10);
mMap.animateCamera(update);
}
// To rmove All the Markers
public void onClick_Remove(View v){
mMap.clear();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
public class MapLocation {
public MapLocation(double lt, double ln, String t){
lat = lt;
lon = ln;
title = t;
}
public double lat;
public double lon;
public String title;
}
}
Now, what I need to do is, I need to show only markers which is near the device.
Here are the steps what I need to do.
Identify a circle round of the device area (500m)
Identify the Markers inside this circle
Show the identified Markers
Re-identify the circle when the device is moving
Need to change the circle and markers when the device is traveling.
Can I do this in Android? If can please help me to do this or post articles that might help me.
Thanks in advance.

okay i can help you with two links.
first of all i will accept that you know how to get your location from maps.after that for drawing a circle follow this guide
How to replace a circle
after that for the radius i can show you a method by the link so you can follow it and modify it for yourself
Detect nearby places
so follow this and you will get your answers.
EDIT
change your locations to latlng as follows;
for r1:
LatLng POINTA = new LatLng(6.9192,79.8950);
//do the same for the others and put it into array belove.it is simple please do not expect direct answer and work for it.
for(LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD}) {
target.setLatitude(point.latitude);
target.setLongitude(point.longitude);
if(location.distanceTo(target) < METERS_100) {
// bingo!
}
}

The best approach here is to simply have a limit to the distance you need the markers visible:
In my previous project, I was syncing down from a server n number of locations within 100 miles for instance. You see, this makes sure that you ONLY get what you need as opposed to having a lot of locations that you won't need.
So, I sync down say 15 nearby locations and add the markers to the map. Then when a user moves, I sync again to make sure I get ONLY the nearby business locations. This worked for me without any problems.
I hope this helps you; so in short:
Sync down n number of locations assuming you know the locations ahead of time in your server (remotely) - you can get the nearest locations using a SQL query (statement). To do this, you will need both latitude and longitude for each location.
Once the syncing is done, simply loop through the data and set the markers - based on the latitude and longitudes of the locations!
That is all you really need!
Good luck!

Surely You can do this.
You can check distance of your device from marker onLocationChanged . Here you can also re-identify circle.

Try this example. It will help you.
http://danielgultom.blogspot.com/2011/01/create-radius-around-point-in-google.html?showComment=1444634924438

Related

I want to replace a Marker from a map when user visits that location ( gets near a Marker )

First, I plot a Marker like this:
public void addMarker(String title,String lat,String Lng,int id,String address,int f)
{
marker= mMap.addMarker(new MarkerOptions().snippet(title)
.title(title+", "+address)
.position(new LatLng(Double.valueOf(lat), Double.valueOf(Lng)))
.icon(BitmapDescriptorFactory.fromResource(id)));
LatLng coordinate = new LatLng(Double.valueOf(lat), Double.valueOf(Lng));
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 10);
mMap.animateCamera(yourLocation);
mMarkerArray.add(marker);
}
After that I am trying to replace the Marker with another icon when ever I reached at any existing Location
#Override
public void onLocationChanged(Location location)
{
Log.d("latitude_main", "onlocation???");
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude_main", "latitude--" + latitude+"longitude="+longitude);
current_lat= String.valueOf(latitude);
current_lng= String.valueOf(longitude);
Log.e("latitude_main","size-=="+salesmanlocationArrayList.size() );
for(int i=0;i<salesmanlocationArrayList.size();i++)
{
if(salesmanlocation.getLati().equals("12.9165757") && salesmanlocation.getLongi().equals("77.6101163"))
{
mMap.addMarker(new MarkerOptions()
.snippet(""+i).title(salesmanlocation.getFirm_name()+", "+salesmanlocation.getAddress())
.position(new LatLng(Double.valueOf(salesmanlocation.getLati().toString()), Double.valueOf(salesmanlocation.getLongi().toString())))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.event_events_select)));
}
mapFragment.getMapAsync(this);
}
}
I want to remove the marker from the map when the user visits that location.
You can simply define one OnMyLocationChangeListener class that performs your tasks, and set it on your GoogleMap instance, this way you can use it whenever you want in your application.
Step 1 - define your listener
public class MyMarkerLocationListener implements GoogleMap.OnMyLocationChangeListener {
List<Marker> markerList;
int MY_DISTANCE;
GoogleMap mMap;
public MyMarkerLocationListener(List<Marker> markerList, int meters, GoogleMap mMap)
{
this.markerList = markerList;
this.MY_DISTANCE = meters;
this.mMap = mMap;
}
#Override
public void onMyLocationChange(Location location) {
// your code/logic
//...
Location myNewLocation = location;
Location someMarkerLocation = new Location("some location");
//for each marker on your list
//check if you are close to it
for (Marker m : markerList) {
LatLng markerPosition = m.getPosition();
someMarkerLocation.setLatitude(markerPosition.latitude);
someMarkerLocation.setLongitude(markerPosition.longitude);
if (myNewLocation.distanceTo(someMarkerLocation) < MY_DISTANCE) {
//remove marker
m.remove();
//or if you still want to use it later
//m.setVisible(false);
// add your new marker
//mMap.addMarker(new MarkerOptions().icon()....);
}
}
}
}
After defining your class you just set the listener on your map on your fragment or activity code =)
Step 2 - instanciate the listener and set it
MyMarkerLocationListener myListener = new MyMarkerLocationListener(mMarkerArray, 100, mMap);
mMap.setOnMyLocationChangeListener(myListener);
UPDATE to answer your question in the comments:
You should initialize mMap before using it, take a look at this piece of code from this Stackoverflow question
public class MapPane extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
don't forget your activity should implement the OnMapReadyCallback interface so the onMapReady method is called
you can use the map only after it is ready
Hope this helps!

control user gesture on my map

I new in coding google map,
my question is how i control the user gestur drag , zoom in and zoom out.
because my code always back to the current location of user when i zoomin/out, nad when i drag/ scroll up, down, left, right. always back to the current possition .
its my code for current loc user
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
}
};
You can use a boolean to move the camera only the first time:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
private GoogleMap mMap;
private Marker mMarker;
private boolean firstTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
if (firstTime) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
firstTime = false;
}
}
}
NOTE: Take into account that this example uses GoogleMap.OnMyLocationChangeListener only because that is the method that you are using in your question, but it's deprecated and you must use the FusedLocationProviderApi according to the documentation:
public final void setOnMyLocationChangeListener
(GoogleMap.OnMyLocationChangeListener listener)
This method was deprecated. use
com.google.android.gms.location.FusedLocationProviderApi instead.
FusedLocationProviderApi provides improved location finding and power
usage and is used by the "My Location" blue dot. See the
MyLocationDemoActivity in the sample applications folder for example
example code, or the Location Developer Guide.

Adding Multiple Markers Google Maps

I am trying to add a marker all depending on what activity the user is on. For example, if the user is in the location1 activity and clicks the button to open maps, it should open Google Maps with a marker at where location1 is. Alternatively, if the user is in the location2 activity and clicks the button to open maps, it should open Google Maps with a marker at where location 2 is.
I have it working when they click on one activity, it brings them to Google Maps and has a marker at where that location is. I have simply tried to copy the code and paste it below with the edited names in it, but if I click a different activity to go to maps, it brings me to the same marker as previously.
My Code for GoogleMapsActivity is below:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Add a marker at the Oval and move the camera
LatLng oval = new LatLng(53.3484013, -6.2605243);
mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));
/*
// Add a marker at Diceys and move the camera
LatLng diceys = new LatLng(53.3358088,-6.2636688);
mMap.addMarker(new MarkerOptions().position(diceys).title("Diceys Nightclub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(diceys));
*/
}
public void changeType(View view)
{
if(mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL)
{
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}}
As you can see, I have commented out the code where I have tried adding a marker for a different location, but it seems to bring me to the same location as above.
Not sure if this is something simple or not as I am new to Google Maps in Android.
Any help would be greatly appreciated.
Thank you.
ArrayList<MarkerData> markerArray = new ArrayList<MarkerData>();
for(int i = 0 ; i < markersArray.size() ; i++ ) {
createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(), markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}
....
protected void createMarker(double lat, double lon, String title, String snippet, int iconResID) {
return googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.anchor(0.5f, 0.5f)
.title(title)
.snippet(snippet);
.icon(BitmapDescriptorFactory.fromResource(iconResID)));
}
Tying it all together:
ArrayList<LatLng> locations = new ArrayList();
locations.add(new LatLng(30.243442, -1.432320));
locations.add(new LatLng(... , ...));
.
.
.
for(LatLng location : locations){
mMap.addMarker(new MarkerOptions()
.position(location)
.title(...)
}
In First Location activity:
Intent i = new Intent(FirstLocationActivity.this, MapsActivity.class);
String keyLatitude = ""; //enter the value
String keyLongitude = ""; //enter the value
i.putExtra("latitude", keyLatitude );
i.putExtra("longitude", keyLongitude );
startActivity(i);
similarly do the same for activity two location, add its corresponding latitude and longitude in extra
In your mapsActivity,
String latitude="";
String longitude ="";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
Bundle bundle = getIntent().getExtras();
latitude = bundle.getString("latitude");
longitude = bundle.getString("longitude");
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Add a marker at the Oval and move the camera
LatLng oval = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(oval).title("Oval Pub"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(oval));
}

Restrict android map marker drag to polyline

I'm looking for some suggestions on how I could approach a problem I'm facing with an android SupportMapFragment. I am drawing a polyline on a SupportMapFragment using LatLng co-ordinance stored in my apps db. I am also adding a map marker at the first and last LatLng co-ordinance to signify the start and end of the route. I would like to provide my users with the ability to trim the route by dragging the start and end markers to their desired points on the polyline. The problem I am facing is restricting the path the markers can be dragged across so they can only be moved along the polyline.
This questions is a little bit old, but I hope others would find this answer useful.
boolean PolyUtil.isLocationOnEdge will help you determine wether a point overlaps a Polygon or Polyline on the map.
public class RestrictedMarkerDragActivity extends FragmentActivity implements
OnMapReadyCallback,
GoogleMap.OnMarkerDragListener {
private static final float WIDTH = 4;
private static final double TOLERANCE_IN_METERS = 3.0;
private GoogleMap map;
private SupportMapFragment mapFragment;
private Polyline polyline;
private Marker marker;
private LatLng positionOnPolyline;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_maps);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
this.map = googleMap;
List<LatLng> points = new ArrayList<>();
points.add(new LatLng(19.35853391311947, -99.15182696733749));
points.add(new LatLng(19.34384275931999, -99.1546209261358));
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.CYAN);
polylineOptions.width(WIDTH);
polyline = map.addPolyline(polylineOptions);
polyline.setPoints(points);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.draggable(true);
markerOptions.position(points.get(0));
marker = map.addMarker(markerOptions);
positionOnPolyline = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
map.setOnMarkerDragListener(this);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(points.get(0), 15));
}
#Override
public void onMarkerDragStart(Marker marker) {
//Nothing to do here
}
#Override
public void onMarkerDrag(Marker marker) {
//If the marker overlaps the polyline, polyline width gets bigger and marker position gets updated,
//else, polyline width remains the same
if(PolyUtil.isLocationOnEdge(marker.getPosition(), polyline.getPoints(), true, TOLERANCE_IN_METERS)) {
polyline.setWidth(WIDTH * 3);
positionOnPolyline = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
} else {
polyline.setWidth(WIDTH);
}
}
#Override
public void onMarkerDragEnd(Marker marker) {
//We set the marker to its last known position over the polyline
marker.setPosition(positionOnPolyline);
//We set polyline width to its original
polyline.setWidth(WIDTH);
}
}

How to add 2 listeners to map?

i'm developing my first app and i created the following map viewer activity:
public class MapViewer extends Activity implements OnInfoWindowClickListener, ClusterManager.OnClusterClickListener<MyItem> {
private GoogleMap map;
private LatLng defaultLatLng = new LatLng(X, Y);
private int zoomLevel = 5;
private Database db = new Database(this);
private ClusterManager<MyItem> mClusterManager;
private LatLngBounds allowedBounds;
private final LatLng northeast = new LatLng(A, B);
private final LatLng southwest = new LatLng(C, D);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapviewer);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(northeast);
builder.include(southwest);
allowedBounds = builder.build();
try {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map != null) {
map.setMyLocationEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setRotateGesturesEnabled(false);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));
mClusterManager = new ClusterManager<MyItem>(this, map);
mClusterManager.setRenderer(new MyClusterRenderer(this, map, mClusterManager));
mClusterManager.setOnClusterClickListener(this);
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
map.setInfoWindowAdapter(new ClusterInfoWindow(getLayoutInflater()));
map.setOnInfoWindowClickListener(this);
addItems();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
As you can see i set a listener to map object
map.setOnCameraChangeListener(mClusterManager);
that adds or removes clusters on markers groups, according to zoom level.
Now i would add a listener that checks if user moves on map within some bounds:
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
checkBounds();
}
});
But it doesn't work. It works only if i remove the previous listener (mClusterManager).
So, how to make both listener working on the same map object?
Thank you in advance for your replies and sorry for my english.
As there's only a set method and no add method, you can only set one listener at a time. But you could delegate from the one listener to the other like this:
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
checkBounds();
mClusterManager.onCameraChange(cameraPosition);
}
});
Of course mClusterManager does not need to implement the CameraChangeListener interface any more but just needs a method public void onCameraChange(CameraPosition cameraPosition).

Categories

Resources