Hi I am trying to change the camera in different locations with an animation. What happens is that when the map opens it zooms to a location then it stops for 6 secs and then it moves to another location with a camera animation. My question is that when this is finished how can I create a 3rd location and then move the camera from the 2nd location to the 3rd? Any idea guys ? Thanks. Here is my code.
public class THEMAP extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
LatLng loc1 = new LatLng(41.889, -87.622);
LatLng loc2 = new LatLng(45.889, -87.622);
LatLng loc3 = new LatLng(49.889, -87.622);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc1, 10));
map.animateCamera(CameraUpdateFactory.zoomIn());
map.animateCamera(CameraUpdateFactory.zoomTo(10), 6000, null);
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.green))
.position(loc2)
.flat(true)
.rotation(245));
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.green))
.position(loc1)
.flat(true)
.rotation(245));
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.green))
.position(loc3)
.flat(true)
.rotation(245));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(mapCenter2)
.zoom(8)
.bearing(90)
.tilt(30)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition ),
8000, null);
}
}
The problem is that if i add another location+marker and try to do the above code, the map animates only the 3rd location ignoring the animation of the 2nd location. Any advice guys ?Please help.
CameraPosition cameraPosition2 = new CameraPosition.Builder()
.target(loc3)
.zoom(8)
.bearing(90)
.tilt(30)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition2 ),
8000, null);
Here's your answer: http://ddewaele.github.io/GoogleMapsV2WithActionBarSherlock/part3
It seems you need to implement the second animation in the onFinish() callback of the second animation.
Related
I'm using the Google Maps API and I want to set my camera on a specific marker. When I do this and I start rotating my camera is losing focus on my marker while they both have the same coordinate.
Does someone knows why this is happening?
My code
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
player.setPosition(myCoordinates);
CameraPosition currentPosition = map.getCameraPosition();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myCoordinates)
.bearing(currentPosition.bearing)
.zoom(18)
.tilt(currentPosition.tilt)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Click here for a video of the problem. (The orange marker is one that the camera is set on)
how can i set the zoom level of a SupportMapFragment ?
I tried some answers I found in similar questions, like this one answered buy a guy named Rob who proposed:
LatLng currentLocation = mService.getCurrentLocation();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(currentLocation) // Sets the center of the map to the current location of the user
.zoom(17) // 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(); // Creates a CameraPosition from the builder
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
But this solution didn't work. So, how can I set the Zoom level knowing that I use a SupportMapFragment.
if your current location is not null then try this code it will work
LatLng currentLocation = mService.getCurrentLocation();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentLocation , 16);
mMap.addMarker(new MarkerOptions().position(location).title(""));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation ));
mMap.animateCamera(cameraUpdate)
Marker currentMarker;
MarkerOptions marker = new MarkerOptions().position(new
LatLng(Double.
parseDouble(lat),Double.parseDouble(lon))).title(title);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
currentMarker = map.addMarker(marker);
currentMarker.showInfoWindow();
currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)), 15));
I was trying to place a marker in Google Map using the following code.
googleMap.clear();
//Dont know why this is 4.8f...
googleMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.google_marker))
.position(ll).flat(true)
.anchor(0.5f, 0.5f));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(ll)
.zoom(initMapZoom) // Sets the zoom
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
cameraPosition = new CameraPosition.Builder()
.target(ll)
.zoom(finalMapZoom) // Sets the zoom
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Now, If I zoom in / out the marker moves from the actual place. If I just remove this
.anchor(0.5f, 0.5f));
Its working fine. The problem is I would like to put the marker in the upper/top position of the screen. So I used .anchor(0.5f, 4.8f)); which is also wrong according to the documentation . The value should be less or equal to 1.
Any ideas to resolve this issue??
Thanks in Advance.
I am using this code for load google map
private GoogleMap myMap;
if(myMap!=null){
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
marker = myMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(getAddress(latitude, longitude)));
Location = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder().target(Location).zoom(15)
.bearing(90) // Sets the orientation of the camera to
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
// myMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15));
}
but map is not loading and occurring this issue
You must install the latest Google play services in your phone/tab/emulator before running the app.
For google play on emulator.
I'm trying to start google maps in my position, but I can not, but I can put a marker at the position you want it to be Initial. I need your help please. I searched a lot here, but nothing has worked for me.
Best Regards
Code
#TargetApi(Build.VERSION_CODES.GINGERBREAD) #SuppressLint("NewApi") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
getSreenDimanstions();
fragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map = fragment.getMap();
bNavigation = (Button) findViewById(R.id.bNavigation);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
miPosicion = new LatLng(latitude, longitude);
map.addMarker(new MarkerOptions().position(miPosicion).title("Start"));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
obtenerItems(map);
}
I used this documentation which uses:
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 17.0f);
theMap.animateCamera(update);
If you want to get the exact current location, you will be better off calling
requestLocationUpdates(..)
rather than
getLastKnownLocation(..)
and then implementing a LocationListener to listen to changes in location and act accordingly. For more information regarding LocationListener refer:
http://developer.android.com/reference/android/location/LocationListener.html
And on a side note: you seem to have initialized two LatLng types(latLng and miPosicion) with the exact same value and have not put either to full use.