Hi I have the above code which animates the camera to a certain location. Is there anyway that I could move the camera position to a different position once it has finished the animation from the first location ? I am trying an if statement but it doesn't work.
for instance:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(1stlocation)
.zoom(12)
.bearing(300)
.tilt(30)
.build();
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.green))
.position(1st Location)
.flat(true)
.rotation(245));
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition ),
30000, null);
//if statement to change the position of the camera when it reaches the first location then move it to another.
CameraPosition campos = map.getCameraPosition();
if(campos == cameraPosition )
{
map.moveCamera(CameraUpdateFactory.newLatLngZoom(2ndloc, 3));
}
Register for a call back onFinish, then just call your next animation. You could chain them with a simple counter.
map.animateCamera(CameraUpdateFactory.newLatLng(cameraPositiom), new CancelableCallback(){
#Override
public void onFinish(){
googleMap.getUiSettings().setScrollGesturesEnabled(true);
}
#Override
public void onCancel(){
googleMap.getUiSettings().setAllGesturesEnabled(true);
}
});
Related
Is there any way by which we can show the particular country as default country in google map?
I have tried with the centre of some of the countries but it seems tedious.
I have only country name say "India" and I need to do is, show "India" in focus when the app gets open.
In your onMapReady() function set
LatLngBounds boundsIndia = new LatLngBounds(new LatLng(23.63936, 68.14712), new LatLng(28.20453, 97.34466));
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundsIndia, padding);
mMap.animateCamera(cameraUpdate);
When you open your map you will see only India country on map.
You can do it for any other countries.
This shows Sydney and puts a marker on it https://developers.google.com/maps/documentation/android-api/map-with-marker
public class MapsMarkerActivity extends AppCompatActivity
implements OnMapReadyCallback {
// Include the OnCreate() method here too, as described above.
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I have make a Map with 30 Overlays that will zoom in at start.
But the Animation (zoom in) is showing before the tiles from the Map are showing. After the Animation, Map is loading the tiles. It is happening everytime. Also after a destroy and reload.
Is there any way to say that the Animation starts after the tiles are loaded ?
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(52.33101, 7.91667 )) // Sets the center of the map to Mountain View
.zoom(9)
.bearing(0)
.tilt(60)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 10000, null);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(52.2056, 7.70862))
Try following:
mMap.setOnMapLoadedCallback(this);
The method can look something like:
#Override
public void onMapLoaded() {
if (mMap != null) {
// animate the camera to desired position
}
}
For more info, following is the link:
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback
I am creating markers based on my location and I would like to make the markers draggable within 1km of that location.
So, basically I want to know how to make a marker draggable within a certain area/region only and not the entire map.
Thank you
You have to define bounds and check if the new position is outside the bounds in the onDragListener of the marker: if the new position is outside the bounds you can reset the marker to the last valid position or do whatever you want.
To match if the marker is-or-not inside the bounds you must use the method contains of the LatLngBounds object.
Something like this:
LatLngBounds bounds;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
// You construct your bounds in a way like this
builder.include(new LatLng(latitude1, longitude1));
builder.include(new LatLng(latitude2, longitude2));
builder.include(new LatLng(latitude3, longitude3));
// etc
bounds = builder.build();
googleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
#Override
public void onMarkerDragStart(Marker marker) {
}
#Override
public void onMarkerDrag(Marker marker) {
}
#Override
public void onMarkerDragEnd(Marker marker) {
if (bounds.contains(marker.getPosition())) {
// cast your spell
} else {
// you can't drag here
}
}
});
Guys I have to show markers on google maps with zoom.When user zoom in or zoom out markers will be visible covering the map area which is on the screen.How can I do this using Android google map?
Okay so you can use a CameraChangeListener:
private HashMap<Integer, Marker> courseMarkers = new HashMap<Integer, Marker>();
//all your visible markers
ArrayList<Item> yourMarkerList = new ArrayList<Item>();
//method to add all your markers with unique ids
addItemsToMap(); //first call to initially show the markers you want
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
private float currentZoom = -1; //keep track of your current zoom level
#Override
public void onCameraChange(CameraPosition camera) {
if (camera.zoom != currentZoom){
currentZoom = camera.zoom;
//here you will then check your markers
addItemsToMap(yourMarkerList);
}
}
});
You will need a class Item that has a variable for a unique int id and a MarkerOptions
private void addItemsToMap(List<Item> items)
{
if(this.mMap != null)
{
//This is the current user-viewable region of the map
LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;
//Loop through all the items that are available to be placed on the map
for(Item m : item)
{
//If the item is within the the bounds of the screen
if(bounds.contains(item.getMarker().getPosition()))
{
//If the item isn't already being displayed
if(!courseMarkers.containsKey(item.getId()))
{
//Add the Marker to the Map and keep track of it with the HashMap
//getMarkerForItem just returns a MarkerOptions object
this.courseMarkers.put(item.getId(), this.googleMap.addMarker(item.getMarker())); //getmarkerforitem
}
}
//If the marker is off screen
else
{
//If the course was previously on screen
if(courseMarkers.containsKey(item.getId()))
{
//1. Remove the Marker from the GoogleMap
courseMarkers.get(item.getId()).remove();
//2. Remove the reference to the Marker from the HashMap
courseMarkers.remove(item.getId());
}
}
}
}
}
This should do it
private void pointToPosition(LatLng position) {
//Build camera position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(15).build();
//Zoom in and animate the camera.
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
I am able to display a marker as well as showing it with zoom and camera setting when first time user is viewing the map. But my requirement is to move the camera to same marker position(when user want) if user goes away from that marker position(marker gets off-screen) during his/her visit.
Having a reference to the GoogleMap object and to the Marker, you can simply use
GoogleMap mMap;
Marker mMarker;
[...]
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMarker.getPosition(), 14));
(where you would substitute the "14" for your desired level of zoom).
Just attach that line to the OnClick event of the button that the user will click to "get back" to the marker... and you're done! ;)
Thanks for replies,
but i was looking for some native Map component to perform the map marker reset task rather than an external button to navigate back to desired marker location. I got this working with the latest update in Map Api(to have setOnMyLocationButtonClickListener) Using below code:-
mMap.setMyLocationEnabled(true);
LatLng markerLoc=new LatLng(companyDetail.getLatitude(), companyDetail.getLongitude());
final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(markerLoc) // Sets the center of the map to Mountain View
.zoom(13) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); //
mMap.addMarker(new MarkerOptions().position(new LatLng(companyDetail.getLatitude(), companyDetail.getLongitude())).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick() {
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return true;
}
});
you can use [animateCamera][1] function of GoogleMap object
GoogleMap googleMap = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map).getMap();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
[1]: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#animateCamera%28com.google.android.gms.maps.CameraUpdate%29
You can also use like this :
LatLng cur_Latlng=new LatLng(21.0000,78.0000); // giving your marker to zoom to your location area.
gm.moveCamera(CameraUpdateFactory.newLatLng(cur_Latlng));
gm.animateCamera(CameraUpdateFactory.zoomTo(4));
// another method is to use current location
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 4);
gm.animateCamera(cameraUpdate);
Marker myMarkerthirtyfour = gm.addMarker(new MarkerOptions()
.position(latLng)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
locationManager.removeUpdates(this);
}