Animating car Marker movement on map V2 - android

I am animating car moving on a street with real time location updates. But the car is facing to true north and the bearing returns 0.0 no matter which direction I am moving. Is there any hack to make the front of my car moving the direction I am moving. Here is my code.
private void draw_current_location(Location currentlocation) {
LatLng current = new LatLng(currentlocation.getLatitude(), currentlocation.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(current, 15);
map.animateCamera(cameraUpdate);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
Location calculatedLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(locationManager.getProvider(LocationManager.GPS_PROVIDER).supportsBearing()) {
float bearing = calculatedLoc.bearingTo(currentlocation);
centeredMap(current, bearing);
}
}
void centeredMap(final LatLng lalng, float bearng){
Location l = new Location("");
l.setLatitude(lalng.latitude);
l.setLongitude(lalng.longitude);
View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
if (customMarker != null) customMarker.remove();
customMarker = map.addMarker(new MarkerOptions()
.position(lalng)
.title("Title")
.snippet("Description")
.anchor(0.5f, 0.5f)
.flat(true)
.rotation(bearng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.customIMAGE)));
Toast.makeText(getApplicationContext(), String.valueOf(l.getBearing()), Toast.LENGTH_SHORT).show();
customMarker.setPosition(lalng);
animation.animateMarker(l, customMarker);
}
UPDATE: Now the bearing is returning values. After I edited my code as such.
if(locationManager.getProvider(LocationManager.GPS_PROVIDER).supportsBearing()) {
Location calculatedLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
float bearing = calculatedLoc.bearingTo(currentlocation);
Toast.makeText(getApplicationContext(), String.valueOf(bearing), Toast.LENGTH_SHORT).show();
centeredMap(currentlocation, bearing);
}
But the marker is rotating along the center vertical axis because of the anchor(.5f, .5f) but it doesn't RECOGNIZE my car marker's front so it doesn't animate along the street. The car is simply static image facing north and being dragged when I move. Any help would be much appreciated.

I got it to work. I was gonna pull my hairs on this one. I was even gonna draw multiple cars with different rotation and animate between all using a switch case or something. But I didn't go that far. I had a static car facing north with anchor at (.5,.5) and drove to a distance to actually matter then it starts to rotate and follow my direction.
1) The bearing is only accurate if there is few seconds difference between the first location and the second. (The closer the location updates the less accurate the bearing)To do that it is better to grab locations further apart and animate direction based on the bearing you get; which is not real time animation(with some latency but great animation that looks like real).
2)You have to use GPS provider to get bearing.

Related

Rotate google map according to bearing with fix marker in center

I am trying to implement google maps in my application.
Requirement:
While driving my marker icon should be fixed in center of screen (facing towards north always) while the map should move left right bottom and rotate when user turn on a street.
How I am trying to do it as
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 5));
float bearing = getBearing(Lastposition,newPosition);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(newPosition)
.zoom(15)
.bearing(bearing)
.tilt(0)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
public static float getBearing(LatLng from, LatLng to) {
Location fromLocation = new Location("");//provider name is unnecessary
fromLocation.setLatitude(from.latitude);
fromLocation.setLongitude(from.longitude);
Location toLocation = new Location("");//provider name is unnecessary
toLocation.setLatitude(to.latitude);
toLocation.setLongitude(to.longitude);
Log.i("Bearing",fromLocation.bearingTo(toLocation)+"");
return fromLocation.bearingTo(toLocation);
}
Results:
Marker moves on the path but camera itself does not move according to driver turns on streets. I hope i was able to make things understandable.Please help out what can be issue or better solution for it.Thanks

Osmdroid maximum zoom level displaying two geo locations

I am using osmdroid library to render maps. I have two geo points that vary intermittently, and want the org.osmdroid.views.MapView to resize and translate making sure both points are always visible. I can easily re-centre the map on the point mid-way between the two, but how do I set the zoom level to ensure my two points are visible?
UPDATE
//Getting pickup and destination latitude and longitude
GeoPoint pickupLocation = null;
if (tripRequest.getPickupLat() != null && tripRequest.getPickupLong() != null) {
pickupLocation = new GeoPoint(tripRequest.getPickupLat(), tripRequest.getPickupLong());
}
GeoPoint destinationLocation = null;
if (tripRequest.getDestinationLat() != null && tripRequest.getDestinationLong() != null) {
destinationLocation = new GeoPoint(tripRequest.getDestinationLat(), tripRequest.getDestinationLong());
}
if (destinationLocation != null && pickupLocation != null) {
//Adding destination marker to map
Marker destinationLocationMarker = new Marker(map);
destinationLocationMarker.setPosition(destinationLocation);
destinationLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
destinationLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_green_small));
map.getOverlays().add(destinationLocationMarker);
//Adding pickup marker to map
Marker pickupLocationMarker = new Marker(map);
pickupLocationMarker.setPosition(pickupLocation);
pickupLocationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
pickupLocationMarker.setIcon(context.getResources().getDrawable(R.mipmap.pin_red_small));
map.getOverlays().add(pickupLocationMarker);
BoundingBox boundingBox = BoundingBox.fromGeoPoints(Arrays.asList(destinationLocation, pickupLocation));
mapController = map.getController();
mapController.setCenter(boundingBox.getCenter());
mapController.setZoom(10);// I need to set this zoom properly
touchListener = new TouchListener();
map.setOnTouchListener(touchListener);
Use zoomToBoundingBox. It should work and if it doesn't problem will be elsewhere.
Try:
map.zoomToBoundingBox(boundingBox, false);
map.invalidate();
It this still don't work, check this response:
zoom mapView to a certain bounding box on osmdroid
If you really want to calculate zoom yourself you can always find an inspiration in source code of zoomToBoundingBox method and modify the calculation to fit your needs.
I believe your are looking for zoom to bounds which is in mapview or map controller. Use the max/min lat lon from your points. You'll want to test at the date line and equator

Calculate radius of visible map user in Google map

What I want to do is calculate a circle's radius by which the user is viewing the map.
I've written the solution so far as follows (which is true):
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
String TAG = AppController.TAG;
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
LatLng target = cameraPosition.target;
LatLng northEast = bounds.northeast;
LatLng southEast = bounds.southwest;
float[] results1 = new float[1];
float[] results2 = new float[1];
Location.distanceBetween(target.latitude, target.longitude, northEast.latitude, northEast.longitude, results1);
Location.distanceBetween(target.latitude, target.longitude, southEast.latitude, southEast.longitude, results2);
double distance = results1[0] > results2[0] ? results1[0] : results2[0];
Log.d(TAG, "onCameraChange:" + results1[0] + " " + results2[0]);
}
});
I'm facing two questions here:
1- First of all why the distane between the center and north east isn't equal to south west?
2- Is there any built in method to achieve the same result?
as you can see from the map above, the horizontal line (latitude) gets shorter the further away you get from the equator.
on the screen, the map is 'distorted' to flatten the curved surface of the earth, with +/- the same amount of longitude and latitude from the center of the view.
so unless you are taking measurements directly at the equator, you will have slight differences in the radius. for small distances, this is negligible.
if accuracy is not important, you can try using the smaller radius (if you want to draw a circle on the view that will not be partially hidden), or use the bigger radius (if you want to do calculations for places within that cicle).

Android How to Show direction on mapview

Hello everyone I am developing a prayer app in which I have to show custom map marker which will show kaba direction from current location. I have lat/lang of kaba and i have to rotate or any other method which will tell user that kaba (holly mecca) is in this direction.
Basically I am using needle or arrow as my marker option to show kaba direction. I have also angle of Kaba from current position. I have searched but not found any Satisfactory answer.
please help me.
Thanks in advance.
I am using the following method to point the direction towards destination.
private void updateCameraPosition() {
Location startingLocation = new Location("starting point");
startingLocation.setLatitude(location.getLatitude()); // location is current location
startingLocation.setLongitude(location.getLongitude());
//Get the target location
Location endingLocation = new Location("ending point");
endingLocation.setLatitude(destination.latitude);
endingLocation.setLongitude(destination.longitude);
//Find the Bearing from current location to next location
float targetBearing = startingLocation.bearingTo(endingLocation);
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(),location.getLongitude()))
.bearing(targetBearing)
.tilt(90)
.zoom(mMap.getCameraPosition().zoom)
.build();
//mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(createLatLngBoundsObject(new LatLng(location.getLatitude(), location.getLongitude()), destination), width, (height - 200), padding));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
EDIT: rotating marker.
moveMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(cameraPosition.target.latitude, cameraPosition.target.longitude)).icon(BitmapDescriptorFactory
.fromBitmap(customMarkerDes)).flat(true).anchor(.5f, .5f).rotation(startingLocation.bearingTo(endingLocation)));

Turn (rotate)marker position based on direction over the route

I've a route created along with a polyline over google maps api v2 (Android). My custom marker moves along the route which i achieved by creating a thread. I also get the maneuvers (turn-left, turn-right, etc).
Now as my marker starts moving (a car icon), i want it to turn and navigate through the route. I did manage to rotate it, however i want an accurate solution for this query. right now i hard coded the angles and thus rotating my marker along with the " setRoation() method ". However the rotation isn't as it should be and i cannot get its dynamic angles.
Please Help !
Thanks in advance
Hey see following code for rotate marker as per location
double oldlat = 19.180237, oldlong = 72.855415;
double newlat = 19.180237, newlong = 72.855415;
public void rotatemarker() {
Location prevLoc = new Location("service Provider");
prevLoc.setLatitude(oldlat);
prevLoc.setLongitude(oldlong);
Location newLoc = new Location("service Provider");
newLoc.setLatitude(newlat);
newLoc.setLongitude(newlong);
bearing = prevLoc.bearingTo(newLoc);
map.clear();
map.addMarker(new MarkerOptions()
.position(new LatLng(newlat, newlong))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.anchor(0.5f, 0.5f)
.rotation(bearing)
.flat(true));
oldlong = newlong;
}

Categories

Resources