I have a marker in my Google Maps map that looks like this:
When the user is driving, I want to rotate it based on his driving direction. How can I achieve this? I should probably use previous location and current location coords for calculation, but I have no idea how.
If you use GPS for locating the user then the Location object you get in onLocationChanged contains the bearing.
If you only have the two coordinates (e.g. you only have coordinates from network location provider), you can use Location.bearingTo() to calculate the bearing of two coordinates:
Location prevLoc = ... ;
Location newLoc = ... ;
float bearing = prevLoc.bearingTo(newLoc) ;
If you have a bearing, you can set the rotation of the marker using MarkerOptions.rotation():
mMap.addMarker(new MarkerOptions()
.position(markerLatLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker))
.anchor(0.5f, 0.5f)
.rotation(bearing)
.flat(true));
You have to set the anchor to the point you want to rotate around, and it's also the point you want to be at the position you set to the marker. (0.5, 0.5) is the center of the image.
Related
I have found many discussions about given LatLngBounds, how to get a fit zoom level. But now I meet a problem that I know current location of map center, and I can get what current zoom level is, and I wanna know the LatLng of the north-east corner and the south-west corner of my map view.
Any ideas? Thanks.
You can get the Projection of the map and the VisibleRegion:
VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
Then you can get the VisibleRegion's farLeft, farRight, nearLeft and nearRight coordinates. So, if you need a LatLngBounds you can do:
LatLngBounds mapBounds = new LatLngBounds(visibleRegion.farLeft, visibleRegion.nearRight);
I'm trying to detect the coordinates of some markers based on current location, but my problem is, sometimes, the accuracy range circle of my current position is relatively large and i have very difficulty to detect those markers because my current location is a little far from the markers. My question is if there is someway to check if the coordinates of those markers are inside the accuracy circle of my current position, or to get the radius distance of the circle?
Accuracy parameter is precisely the radius of a confidence circle. From Android doc:
We define accuracy as the radius of 68% confidence. In other words, if
you draw a circle centered at this location's latitude and longitude,
and with a radius equal to the accuracy, then there is a 68%
probability that the true location is inside the circle.
You can also get distance between two GoogleMap Point doing this:
Location location1 = new Location(point1.latitude, point1.longitude);
Location location2 = new Location(point2.latitude, point2.longitude);
float distanceInMeters = location1.distanceTo(location2);
I am using android Google Map API v2 to build a navigation application on android phone. For direction of movement, I used a marker with arrow image as the marker (the image have a dot and a arrow on top of it). I tried to use .rotation to set orientation of the marker according to the direction of movement.
The problem is that when I rotate Google Map, the Map's rotation change but the orientation of marker is unchanged.
I am looking for a way to set rotation of the marker relative to Map's rotation (so when an user rotate map, the marker will rotate accordingly).
Or if anyone know how to build direction arrow other than my primitive way, please enlighten me. Thank you :)
By default, markers are oriented against the screen, and will not rotate or tilt with the camera. Flat markers are oriented against the surface of the earth, and will rotate and tilt with the camera.
You can make the marker flat by calling flat(true) as in the following example:
Marker perth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.flat(true));
call setRotation and that will do the trick!
e.g.
static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.anchor(0.5,0.5)
.rotation(90.0));
Code Ref
How can I create an overlay image of an arrow that points to the driving direction as such.
The road directions vary significantly, the angle, curve is different from time to time, so the arrow has to be flexible enough to adapt to road condition. How can I do that?
Thanks
Not long ago Google added the Flat Marker option to the Google Maps API V2, take a look here:
https://developers.google.com/maps/documentation/android/marker#flatten_a_marker
You can create it like so:
static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.flat(true)
.rotation(90.0));
and get the rotation parameter by using the bearing sensor of the phone:
I am working on google map v2 and i got a question,
i can now add a single marker for my current location
and i know the concept about adding markers,
but now i want to add more markers which near my current location,
and there are thousands of markers can add into the map due to the database
so how can i add markers within a range of area which is surround my current location,
and the range will be the user's screen size.
Thus more markers when zoom out and less markers when zoom in.
or maybe radius set as 5km of if current location
Thanks guys
marker = map.addMarker(new MarkerOptions().position(
new LatLng(location.getLatitude(), location.getLongitude()))
.title("my position").icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
use this approach:
var lat=new_marker.getPosition().lat();
var lng=new_marker.getPosition().lng();
var target = new GLatLng(lat,lng);
var center= new GLatLng(yourLat, yourLng);//your gps position
var distance = center.distanceFrom(target) / 1000;//meters to km
if(distance < radius){//where radius = 5Km
new_marker.setMap(map);//add to map
}
Look this example is similar that you are looking for, he wants to draw a circle that resize to screen size: Google map api V2 resize screen to match a circle drawn