Android Google Maps API v2: get my bearing location - android

I would like to force the camera to behave like you are using navigation, it means when you rotate 90° left, the camera does the same thing.
I have a Google Map where my location (as a blue dot) is shown.
mGoogleMap.setMyLocationEnabled(true);
When I am moving, blue dot is with an arrow which shows my current bearing. I would like to get this bearing.
I am getting my current location using FusedLocationApi:
currentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Code below is animating camera to current location, but without the bearing. I can add the bearing, but I don't know the value.
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng( location.getLatitude(), location.getLongitude() );
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
Do you know how to get the current location direction? I was unable to find any proper information about that. I would greatly appreciate any help you can give me.
Thank you

I think you can try to use getOrientation(R, orientation) (and adjust for true north) to get the device rotation. You can refer to here.
Also, use the CameraPosition class here to adjust camera position.
EDIT: I even found better solution. Use getBearing() method in Location class.
if (location.hasBearing()) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng) // Sets the center of the map to current location
.zoom(15) // Sets the zoom
.bearing(location.getBearing()) // Sets the orientation of the camera to east
.tilt(0) // Sets the tilt of the camera to 0 degrees
.build(); // Creates a CameraPosition from the builder
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

Related

rotate google maps with respect to the direction of movement of device (android)

i am working on app which use google maps to plot the route in which the user is moving and its showing live. so i want to rotate the map according to the direction of movement of user.
if he/she moves right then the map rotate in such a way that the route he is moving always shows in foreword direction. like google maps navigation.
i think i should take the bearing between two location and then calculate some bearing and apply it to the maps.
any suggestion will be appreciated .
thank you
update
let me clarify my question as it is not that much understandable (thats what i think).
i want the my location pointer to point forward or upward always and the map to rotate itself when i move in an direction.
i think this makes my question more clear.
Calculate bearing between two points and then use it to set the camera position and then animate camera.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(targetLatLng)
.bearing(targetBearing)
.zoom(mMap.getCameraPosition().zoom)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
2000,
mCancelableCallback);
import android.location.Location;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
....
protected void animateToLocation(Location position)
{
if (mGoogleMap == null || position == null)
return;
float zoomLevel = mGoogleMap.getCameraPosition().zoom != mGoogleMap.getMinZoomLevel() ? mGoogleMap.getCameraPosition().zoom : 18.0F;
LatLng latLng = new LatLng(position.getLatitude(), position.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng)
.bearing(position.getBearing())
.zoom(zoomLevel)
.build();
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), null);
}
while position:Location can be fetched from FusedApi#onLocationResult or LocationManager#OnLocationChanged

android google maps animation between 2 locations, advice

Hi I have created a map as well as some markers which indicate places/locations on the map. Is there any possible way of creating an animation between those location? For instance drawing a polyline between 2 different locations and create an animation from a start point to an end point ? Is there any documentation for such a thing or any tutorials ? Many thanks.
Something like this:
https://www.youtube.com/watch?v=HPWYorS68WE
use this code and you can also follow this link
https://developers.google.com/maps/documentation/android/
Flat Markers
LatLng mapCenter = new LatLng(41.889, -87.622);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 13));
// Flat markers will rotate when the map is rotated,
// and change perspective when the map is tilted.
map.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.direction_arrow))
.position(mapCenter)
.flat(true)
.rotation(245));
CameraPosition cameraPosition = CameraPosition.builder()
.target(mapCenter)
.zoom(13)
.bearing(90)
.build();
// Animate the change in camera view over 2 seconds
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
2000, null);
}

How to animateCamera at run time in Map V2

I am new in android, I am making application which have Google Map V2. I want to animateCamera Zoom value at runtime. It should be depend on the current location(latitude and longitude) and Destination Location(destination latitude and longitude).
It should be Zoom such it should show both marker as nearest possible and should not require any pan, zoom in/out gesture on mapv2.
I used this code but I need to pan gesture to see both market on the Map. I want it should be default as nearest possible and should not require any pan gesture.
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
Thanks in advance.
you need to use newLatLngBounds()
Returns a CameraUpdate that transforms the camera such that the specified latitude/longitude bounds are centered on screen at the greatest possible zoom level. You can specify padding, in order to inset the bounding box from the map view's edges. The returned CameraUpdate has a bearing of 0 and a tilt of 0.
figure out the bounding box of the 2 points and give the southwest most point and the north east most point
example:
map.animateCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(southWest,northEast),10));
to get the bounding box you need to loop through your points and find the max/min lat/lng
example:
for(//for loop){
LatLng point = new LatLng(lat,lng)
if(point.getLatitude() > maxLat){
maxLat = point.getLatitude();
}
if(point.getLatitude() < minLat){
minLat = point.getLatitude();
}
if(point.getLongitude() > maxLon){
maxLon = point.getLongitude();
}
if(point.getLongitude() < minLon){
minLon = point.getLongitude();
}
}
northEast = new LatLng(maxLat,maxLon);
southWest = new LatLng(minLat,minLon);

How to get the coordinates of the blue dot in android maps v2

On a Map I want to show the path direction from my location to a certain location. I have implemented the logic for drawing the path on the map already, but I have issues determinate my current location.
I have being using new LocationClient for retrieving my current location just like it is described in this article: http://developer.android.com/training/location/retrieve-current.html , and i have read somewhere that the new maps API v2 is using the same technique for getting my location.
Anyway, when I draw the path directions on the map from my location to the desired one, the starting point of the path is different from the blue dot that marks my current location.
The blue dot shows the right position, and my implementation doesn't. So, is there a way to get the coordinates of the blue dot marker, something like map.getMyLocationMarker().getLocation() or I need to figure out some other way to get my location manually?
UPDATE
I forgot that I have left this question opened, but here is my answer below.
Location findme = mMap.getMyLocation();
double latitude = findme.getLatitude();
double longitude = findme.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
This will give you latitude and longitude of your location.
Since googlemyMap.getMyLocation() method is deprecated...
Best simple solution so far that i found
Getting current location and center the camera on marker.
Android Studio editor see there is a error but when you run it there is no problem with the code.
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria,false));
if (location != null)
{ mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 13));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria,false));
if (location != null)
{
mMap.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
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
LatLng goztepe = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(goztepe).title("Göz Göz GÖZTEPE"));
explained here .
The original answer was to use setMyLocationChangeListener() on GoogleMap, to be notified of "blue dot" changes. That is now deprecated.
If you can create a sample project that reproduces your problem, you may wish to file an issue, attaching that project. What you are doing is the official replacement for the deprecated methods, and so one would hope that your code would work properly. If you do file an issue, and if you think of it, post a link to it here, as I'd like to keep an eye on it.
It was actually a really stupid mistake I did.
But here is the answer. The procedure described in the link in the question is the right thing to do.
My problem was that I saved the fetched data to a new shared preferences key, and I was reading it from the old one where the wrong location was saved. Yeah it is pretty dumb :)
So follow the tutorial on android developer portal and there will be no mistakes with it.

Android Google Maps v2 - set zoom level for myLocation

Is it possible to change the zoom level for myLocation with the new Google Maps API v2?
If you set GoogleMap.setEnableMyLocation(true);, you get a button on the map to find your location.
If you click on it, then the map will bring you to your location and zoom it in to some level. Can I change this zoom to be less or more?
It's doubtful you can change it on click with the default myLocation Marker. However, if you would like the app to automatically zoom in on your location once it is found, I would check out my answer to this question
Note that the answer I provided does not zoom in, but if you modify the onLocationChanged method to be like the one below, you can choose whatever zoom level you like:
#Override
public void onLocationChanged(Location location) {
if( mListener != null ) {
mListener.onLocationChanged( location );
//Move the camera to the user's location and zoom in!
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12.0f));
}
}
You can also use:
mMap.animateCamera( CameraUpdateFactory.zoomTo( 17.0f ) );
To just change the zoom value to any desired value between minimum value=2.0 and maximum value=21.0.
The API warns that not all locations have tiles at values at or near maximum zoom.
See this for more information about zoom methods available in the CameraUpdateFactory.
with location - in new GoogleMaps SDK:
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(chLocation,14));
Here are the approximate zoom levels and what they do :
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings
so you could do something like this to zoom to street level for example (note the "15f" below is street level) :
override fun onMapReady(googleMap: GoogleMap?) {
googleMap?.mapType = GoogleMap.MAP_TYPE_NORMAL
googleMap?.addMarker(MarkerOptions()
.position(LatLng(37.4233438, -122.0728817))
.title("cool place")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)))
googleMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.4233438, -122.0728817), 15f))
note: just so you know different locations can have different max zoom levels. try to use googleMap.maxZoomLevel if you want to get the max or min zoom levels.
Slightly different solution than HeatfanJohn's, where I change the zoom relatively to the current zoom level:
// Zoom out just a little
map.animateCamera(CameraUpdateFactory.zoomTo(map.getCameraPosition().zoom - 0.5f));
In onMapReady() Method
change the zoomLevel to any desired value.
float zoomLevel = (float) 18.0;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
you have to write only one line in maps_activity.xml
map:cameraZoom="13"
I hope this will solve your problem...
You can use
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
Even i recently had the same query....some how none of the above mentioned setmaxzoom or else map:cameraZoom="13" did not work
So i found that the depenedency which i used was old
please make sure your dependency for google maps is correct
this is the newest use this
compile 'com.google.android.gms:play-services:11.8.0'
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPlace,15));
This is will not have animation effect.
I tried with "mMap.animateCamera( CameraUpdateFactory.zoomTo( 17.0f ) );" but it didn't work for me. So I used this animation for zooming in on start.
LatLng loc = new LatLng(33.8688, 151.2093);
mMap.addMarker(new MarkerOptions().position(loc).title("Sydney"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 18), 5000, null);
Location locaton;
double latitude = location.getlatitude;
double longitude = location.getlongitude;
If you want to save the zoom or get it all the time,you just need to call the following
int zoom = mMap.getCameraPosition().zoom;
//To set that just use
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(getlatitude(), getlongitude),zoom);
Most of the answers above are either deprecated or the zoom works by retaining the current latitude and longitude and does not zoom to the exact location you want it to. Add the following code to your onMapReady() method.
#Override
public void onMapReady(GoogleMap googleMap) {
//Set marker on the map
googleMap.addMarker(new MarkerOptions().position(new LatLng(0.0000, 0.0000)).title("Marker"));
//Create a CameraUpdate variable to store the intended location and zoom of the camera
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(0.0000, 0.0000), 13);
//Animate the zoom using the animateCamera() method
googleMap.animateCamera(cameraUpdate);
}

Categories

Resources