I am working on map base application to animate marker. There are one marker which is updated against 30 sec interval from server. Marker always move to center of the Map ,so i close moveCamera marker but when marker move outside the map then marker is not come in map view. so i want to camera move when marker goes from map view
Before setting new position of the marker, check it's new position and current bounds of map view.
LatLng newPosition = new LatLng(...);
boolean contains = mMap.getProjection()
.getVisibleRegion()
.latLngBounds
.contains(newPosition);
if(!contains){
// MOVE CAMERA
}
// UPDATE MARKER POSITION
Edit
I created a sample route to simulate each point on map periodically. Route gist
public class SampleRoute {
public static List<LatLng> GetPoints() {
return new ArrayList<>(Arrays.asList(
new LatLng(38.4670419, 27.1647131),
new LatLng(38.4667244, 27.1648277),
new LatLng(38.4666633, 27.1649079),
new LatLng(38.4665983, 27.1648022),
new LatLng(38.4665958, 27.1647843),
new LatLng(38.4665958, 27.1647843),
new LatLng(38.4665809, 27.1646429),
new LatLng(38.4665704, 27.1645506),
new LatLng(38.4665529, 27.1644067),
...
}
}
}
Then i create a method in sample activity that calculates current region's bounds and marker's X, Y points on this region. Activity gist
private void moveCamera(LatLng destination){
Projection projection = mMap.getProjection();
LatLngBounds bounds = projection.getVisibleRegion().latLngBounds;
int boundsTopY = projection.toScreenLocation(bounds.northeast).y;
int boundsBottomY = projection.toScreenLocation(bounds.southwest).y;
int boundsTopX = projection.toScreenLocation(bounds.northeast).x;
int boundsBottomX = projection.toScreenLocation(bounds.southwest).x;
int offsetY = (boundsBottomY - boundsTopY) / 10;
int offsetX = (boundsTopX - boundsBottomX ) / 10;
Point destinationPoint = projection.toScreenLocation(destination);
int destinationX = destinationPoint.x;
int destinationY = destinationPoint.y;
int scrollX = 0;
int scrollY = 0;
if(destinationY <= (boundsTopY + offsetY)){
scrollY = -(Math.abs((boundsTopY + offsetY) - destinationY));
}
else if(destinationY >= (boundsBottomY - offsetY)){
scrollY = (Math.abs(destinationY - (boundsBottomY - offsetY)));
}
if(destinationX >= (boundsTopX - offsetX)){
scrollX = (Math.abs(destinationX - (boundsTopX - offsetX)));
}
else if(destinationX <= (boundsBottomX + offsetX)){
scrollX = -(Math.abs((boundsBottomX + offsetX) - destinationX));
}
mMap.animateCamera(CameraUpdateFactory.scrollBy(scrollX, scrollY));
mMarker.setPosition(destination);
}
And then started to simulate points
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
moveCamera(mPoints.get(mCurrentPos));
if(++mCurrentPos < mPoints.size()){
mHandler.postDelayed(this, 1500);
}
}
}, 1500);
I tried and it's working well on me
So, if i understand you correctly and it works for you too then i could explain.
When marker location is updated you can animate the camera to the marker position. Following sample code may help you
LatLng definedLoc = new LatLng(latitudeValue, longitudeValue);
CameraPosition cameraPosition = new CameraPosition.Builder().target(definedLoc).zoom(13.0F).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
You can animate the camera to the Marker position if the map bounds does not contain the marker using a GoogleMap.OnCameraChangeListener
:
private Marker marker;
// ...
@Override
public void onCameraChange(final CameraPosition cameraPosition) {
ensureMarkerOnBounds();
}
private void ensureMarkerOnBounds() {
if (marker != null) {
if (!mMap.getProjection().getVisibleRegion().latLngBounds.contains(marker.getPosition())) {
mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
}
}
}
// This is the function that you use to move your marker
private void moveMarker (Marker marker) {
// ... Your code to move your marker
ensureMarkerOnBounds();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With