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

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;
}

Related

How to retrieve latitude and longitude of Google Maps android

How can I retrieve latitude and longitude of Google Maps data from my database and display it to the user on the map in android ?
I have a database mysql containing some fields as longitude and latitude.
What is the best way to do such?
Is it possible through a Volley library?
If there is an example of this, it is good to me
Yes you can draw stuff in the onMapReady callback. You can create Markers, draw circles, draw polygons.
To add a marker just call:
LatLng point = new LatLng(exampleLat, exampleLng);
mGoogleMap.addMarker(new MarkerOptions().position(point));
Here is an example of how to draw a circle:
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(lat, lng))
.radius(radius)
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT);
Circle circle = mGoogleMap.addCircle(circleOptions);
Here is a Polygon:
PolygonOptions polygonalOptions = new PolygonOptions()
.fillColor(Color.RED)
.strokeColor(Color.TRANSPARENT)
.clickable(true);
for (List<Double> latLng : myPolygon.path) {
LatLng point = new LatLng(latLng.get(0), latLng.get(1));
polygonalOptions.add(point);
}
mGoogleMap.addPolygon(polygonalOptions);
PS: Writing from my phone sorry for any mistakes or formatting issues.

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

Animating car Marker movement on map V2

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.

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)));

How to get Latitude/Longitude span in Google Map V2 for Android

I have a task for move my app to Google Maps Android APIs V2. Now, I need to get Latitude/Longitude span. I used MapView.getLatitudeSpan() and MapView.getLongitudeSpan() in previous version APIs. Now I can't find something like this in V2.
Does anybody have the same problem?
You can use the following code to get the lat/lng span:
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
I hope this helps.
First obtain a Projection using GoogleMap.getProjection(). Then you can call Projection.getVisibleRegion() to obtain a VisibleRegion which has a LatLngBounds.
The reason why a LatitudeSpan and Longitude span no longer makes sense is because the map can now be rotated and tilted and so viewport is no longer a latitude/longitude aligned rectangle on the map.
This way works for me:
CameraPosition camPos2 = mapa.getCameraPosition();
LatLng pos = camPos2.target;
Toast.makeText(MainActivity.this,"Lat: " + pos.latitude + " - Lng: " +pos.longitude, Toast.LENGTH_LONG).show();
Oops, i misunderstood the question, i mean i did not saw "span" word.
According the API the correct would be:
First get the bounds:
LatLngBounds bounds = gMap.getProjection().getVisibleRegion().latLngBounds;
And then ask if any point is in bounds:
LatLng point = new LatLng (latitude, longitude);
if(bounds.contains(point)){
//do something
}
Here is the answer
LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
if (bounds.contains(ROMA)) {
marker = googleMap.addMarker(
new MarkerOptions()
.position(ROMA)
.title("Hello")
.snippet("Nice Place")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
);
System.out.println("Marker added");
}
Add the marker only when it falls in the visible region

Categories

Resources