How to move android google map camera with constant velocity and zoom - android

I cannot find the way to move android google map camera with constant velocity and zoom.
For example, if the current zoom is 15, and calling
CameraPosition pos= CameraPosition
.builder(map.mMap.getCameraPosition())
.target(target)
.zoom(15)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos), duration * 1000, null);
it modifies the zoom 15 - > 12 -> 15
(the mid value depends on the distance)
My target is to show an object moving on the map with constant velocity and always keep it in the center of the map
Tried to call moveCamera in the ValueAnimator of the object, but then the map jumps. I need to move it smoothly

Use fused API for location update and move the camera to the updated location
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<android.location.Location>() {
#Override
public void onSuccess(android.location.Location location) {
if(location!=null){
try{
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
String addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1).get(0).getAddressLine(0);
marker_origin = new MarkerOptions().
position(latLng).title(addresses)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mMap.addMarker(marker_origin);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));
getPlacesData(latLng, "restaurant");
}catch (Exception e){
e.printStackTrace();
}
}
}
});
For further details check the android developer documentation
https://developer.android.com/training/location/receive-location-updates.html

Related

How to Zoom and get latitude and longitude from mMap.setMyLocationEnabled(true) in Google Map (Android)

I was setting mMap.setMyLocationEnabled(true) and I am successfully getting my current location but...
Not able to zoom to that position.
Not able to get lat and lng from that position
From getting latitude and longitude I am using below code but it gives me null. I am calling below method from onMapReady(Google Map) and passing google map object.
private void goToCurrentLocation(GoogleMap map) {
Log.d("Tag", "inside");
Location loc = map.getMyLocation();
if (loc != null) {
LatLng point = new LatLng(loc.getLatitude(), loc.getLongitude());
Log.d("Tag=", "latlng=" + point);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point, 15));
}
}
Is there any alternative to getting latitude and longitude as getMyLocation deprecated?
use link provided in the answer to get current location and then zoom over that location using below code:
latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
mGoogleMap.animateCamera(cameraUpdate);
http://blog.teamtreehouse.com/beginners-guide-location-android

Android map pin move position during zoom in / zoom out

In my android app, I have added google map and I load dynamically more points using specific lat / long values and specific pins
This is my code:
GoogleMap map = holder.mapView.getMap();
if(map != null)
{
try {
MapsInitializer.initialize(activity);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
map.clear();
map.getUiSettings().setZoomControlsEnabled(true);
LatLng latLng=new LatLng(gS.getLat(), gS.getLon());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
CameraUpdate center=
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(10);
//map.animateCamera(cameraUpdate);
map.moveCamera(cameraUpdate);
map.animateCamera(zoom);
if(time_diff > day3 && magnitude >= 0 && magnitude < 2)
map.addMarker(new MarkerOptions()
.draggable(false)
.anchor(0, 1)
.position(new LatLng(gS.getLat(), gS.getLon()))
.title("").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_lt2m)));
The code works fine and loads the data into map with custom pin, but I have seen that when I zoom out or zoom in the map, the marker moves its position instead of remaining in its actual lat long position.
Any help?
Thanks
I use API V2. it's possible that a google play service old version create this?

Location query in Google maps in android

I have made an app utilizing Google Maps. I have added a marker of my current location in it. However, as soon as I open the app, I want it to zoom in to my location instead of showing the whole world at the 95302 23535919711008489 location first. How do I accomplish this?
Use animateCamera() as below
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
Please read the docs
LatLng cameraLatLng = new LatLng(savedLat, savedLng);
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
googleMap.animateCamera(CameraUpdateFactory
.newLatLngZoom(cameraLatLng, 7))
savedLat, savedLng your current location longitude and latitude.You can change zoom number ( 7 ) to your fitting.

How to stop zooming in google map api 2 after updating current location every second?

I have developed an android app which get users current location at every second. but while updating location every second it zoom in by 2x at every update at every time so please help me to set constant zoom level which will not zoom after updating every second, I used this listener in onCreate
map.setOnMyLocationChangeListener(myLocationChangeListener);
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange (Location location) {
LatLng loc = new LatLng (location.getLatitude(), location.getLongitude());
Bitmap bitmapicon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("My Location").icon(BitmapDescriptorFactory.fromBitmap(bitmapicon)));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 5.0f));
}
};
You have to change the
map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 5.0f));
to
map.animateCamera(CameraUpdateFactory.newLatLng(loc);
try this
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(),loc.getLongitude()), 5.0f));
see this for further reference reference
You need to declare a CameraPosition() object like this:
mCameraPosition = new CameraPosition(mLatLong, 18, 30, 112.5F);
Have a look at the constructor here
CameraPosition(LatLng target, float zoom, float tilt, float bearing)
And finally pass this Camera position to your map like :
mGoogleMapOptions.camera(mCameraPosition);
Then pass the same zoom level when the location changes:
CameraUpdateFactory.newLatLngZoom(LatLng, float)
So in this case your zoom level will be 18 along with the new location.
As stated on this link

How to get the Latitude and Longitude on Map in Android?

How can I get the Latitude and Longitude values of a particular location that I have long clicked on the map in Android?
For the long click, I suggest you check out http://www.kind-kristiansen.no/2010/handling-longpresslongclick-in-mapactivity/. This will go into detail on how to listen for long click events within the Maps API since there is little or no built-in functionality that I know of.
As for the lat/lng code, after you get the long click you can translate the pixels to coordinates.
public void recieveLongClick(MotionEvent ev)
{
Projection p = mapView.getProjection();
GeoPoint geoPoint = p.fromPixels((int) ev.getX(), (int) ev.getY());
// You can now pull lat/lng from geoPoint
}
You'll have to manage the LongClick event, and then use the code to find out longitude and latitude with the following code:
GeoPoint geoPoint=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
int latitude = geoPoint.getLatitudeE6();
int longitude = geoPoint.getLongitudeE6();
where 'event' is the object of 'MotionEvent'.
Use any other event according to your case.
It gives latitude and longitude on which point of map click
map.setOnMapClickListener(new OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
//The code below demonstrate how to convert between LatLng and Location
//Convert LatLng to Location
Location location = new Location("Test");
location.setLatitude(point.latitude);
location.setLongitude(point.longitude);
location.setTime(new Date().getTime()); //Set time as current Date
txtinfo.setText(location.toString());
//Convert Location to LatLng
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(newLatLng)
.title(newLatLng.toString());
map.addMarker(markerOptions);
}
});

Categories

Resources