Set CameraPosition to my location, but show other markers also - android

I am designing an Android app that should show multiple markers on map. I can do that easily. My problem is that I need to show all the markers around my position, and the CameraPosition needs to be in focused in my position. I can easily do each on them separately, but what I need is showing them together, my location in the middle of the map, and the markers around my location.
Any idea how can I achieve that? Thanks in advance!

Set your zoom to a higher level so it can zoom out and see more of your map around your current location.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(yourLatLng).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

The Functionality you need is to keep all the selected markers on the maps and it it should be visible at a single time. you have to keep all those points in the visible region with myLlocation (your current location) at the center of the screen always.
First keep your myLocation inside an latlng variable and all the other latlng which are to be inside the bounds inside another ArrayList of LatLng. Also define some variables to keep track of operation.
LatLng myLocation=null;
ArrayList<LatLng> latlngs = null; // the arraylist of other latlng;
boolean are_all_inside_viewport = false;
float current_zoom = googleMap.getCameraPosition().zoom;
//initialise these variables myLocation and latlngs;
Then animate your camera to myLocation which will be placed into the center of the map. put a low zoom level here.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myLocation)
.zoom(14)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Now check for each latlng in the ArrayList of they are in the google maps view port. if they are inside then its ok. if not then decrease a zoom level of the maps and animate the camera again.
while (!are_all_inside_viewport) {
are_all_inside_viewport = true;
for (LatLng latLng : latlngs) {
if (!bounds.contains(latLng)) {
are_all_inside_viewport = false;
break;
}
}
bounds = googleMap_local.getProjection().getVisibleRegion().latLngBounds;
cameraPosition = new CameraPosition.Builder()
.target(myLocation)
.zoom(current_zoom - 1)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
This will go on checking for each latlng inside the ArrayList until all the latlng are inside the viewport.
Hope this answer helps you.

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

Google Maps Api camera doesn't rotate properly on coordinate

I'm using the Google Maps API and I want to set my camera on a specific marker. When I do this and I start rotating my camera is losing focus on my marker while they both have the same coordinate.
Does someone knows why this is happening?
My code
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
player.setPosition(myCoordinates);
CameraPosition currentPosition = map.getCameraPosition();
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myCoordinates)
.bearing(currentPosition.bearing)
.zoom(18)
.tilt(currentPosition.tilt)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Click here for a video of the problem. (The orange marker is one that the camera is set on)

How to place a Marker on center of the screen on google map

Place marker on center of the screen on google map android same as like in uber and ola apps. When moving or scrolling a google map marker should not move and it should give latlng coordinates
You need to put an ImageView center of frameLayout.That is not marker of your map but it place in the center and when you click that imageview you need to get center LatLng of the map
Here is code for getting center LatLng of map :
LatLng center = mgoogleMap.getCameraPosition().target;
You need to use frame layout to align your marker in this case a image like this at center. and then fetch location using googleMap.getCameraPosition().target
for more info see http://developer.android.com/reference/com/google/android/gms/maps/model/CameraPosition.html#target
Try this,
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(currentLatitude, currentLongitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Check Out This
LatLng mapCoordinate = new LatLng(yourLatitude, yourLatitude);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(mapCoordinate, 11.0f);
yourMap.animateCamera(yourLocation);
Or In Other way
LatLng mapCoordinate = new LatLng(yourLatitude, yourLatitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(mapCoordinate) // set to Center
.zoom(12.0f) // for the Zoom
.bearing(90) // Orientation of the camera to east
.tilt(30) // Tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition
yourMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Google maps - camera position with marker at bottom

If you look at google navigation, it always keeps the driver marker close to bottom and when you move the camera it offers to reset it back to bottom. I'm wondering how to achieve the same thing given a marker.
There was a similar question before, but the offered answers do not consider the map to be tilted which results in wrong projection.
The current location marker always to bottom of the screen while moving also. For that we have to set
map.setPadding(0,320,0,0);
So, we set pdding top to the map 320 then it takes some space from the top of the screen.
In your final code like this
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(newLatLng)
.zoom(18)
.bearing(0)
.tilt(0)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.setPadding(0,320,0,0);
Nima, there are different way to achieve this behaviour by tweaking values in camera positions.
For instance you have 2 geo location latlng information available with you, UserLocation and DestinationLocation find the midpoint of this location and set the camera target at it. And then you can move the camera to zoom level which cover both geolocation with proper padding of top and bottom by specifying the bearing value.
//First build the bounds using the builder
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds;
builder.include(userLocation);
builder.include(destinationLocation);
bounds = builder.build();
// define value for padding
int padding =20;
//This cameraupdate will zoom the map to a level where both location visible on map and also set the padding on four side.
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding);
mMap.moveCamera(cu);
// now lets make the map rotate based on user location
// for that we will add the bearing to the camera position.
//convert latlng to Location object
Location startLocation = new Location("startingPoint");
startLocation.setLatitude(userLocation.latitude);
startLocation.setLongitude(userLocation.longitude);
Location endLocation = new Location("endingPoint");
endLocation.setLatitude(destinationLocation.latitude);
endLocation.setLongitude(destinationLocation.longitude);
//get the bearing which will help in rotating the map.
float targetBearing = startLocation.bearingTo(endLocation);
//Now set this values in cameraposition
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(bounds.getCenter())
.zoom(mMap.getCameraPosition().zoom)
.bearing(targetBearing)
.build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

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

Categories

Resources