11

I'm trying to set the user location on the map such that it sits about 1/3 of the way up from the bottom of the screen, and when the map rotates, it will rotate around this point.

The closest I've got to achieving this is by using the setPadding() method, however, this causes the map to sort of shake when rotated, as the center point sort of 'floats' around where it actually should be. It looks quite ugly

int mapHeight = mapView.getHeight();
googleMap.setPadding(0, mapHeight / 5, 0, 0);

Is there a better way to do this?

Edit: Explained in picture below

enter image description here

Charuක
  • 12,953
  • 5
  • 50
  • 88
Dportology
  • 836
  • 1
  • 9
  • 32

3 Answers3

21

You don't need paddings

Change mappoint X and Y values as you need you can call this where you want! may be inside your onLocationChanged like changeOffsetCenter(location.getLatitude(),location.getLongitude());

 public void changeOffsetCenter(double latitude,double longitude) {
            Point mappoint = mGoogleMap.getProjection().toScreenLocation(new LatLng(latitude, longitude));
            mappoint.set(mappoint.x, mappoint.y-100); // change these values as you need , just hard coded a value if you want you can give it based on a ratio like using DisplayMetrics  as well
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(mGoogleMap.getProjection().fromScreenLocation(mappoint)));
        }

output :

enter image description here

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • @ZeroOne your question is not clear elaborate please? you can have one center point is that what you are asking ? – Charuක Feb 27 '17 at 08:19
  • basically, how to do it for multiple marker which is re-position them at bottom of map? – ZeroOne Feb 27 '17 at 08:23
  • Friend this answer is for a center point not center points you may do that by selecting a main marker at your bottom with a law zoom level(cam is not that zoomed) or go with a solution like where you take center point(LatLng) between some locations and keep it as the center possition.Check this might help > http://stackoverflow.com/a/35646210/5188159 finding a position is not related to this answer but move center point as you want(using x,y) is the one which i addressed here :) – Charuක Feb 27 '17 at 08:41
  • I prefer this : mMap.setPadding(leftPx, topPx, rightPx, bottomPx); >>https://stackoverflow.com/a/20671939/929086 – fingerup Jul 25 '17 at 08:14
  • Instead of using `Point.set()` to set a new value you can use `Point.offset(xOfffset, yOffset)` to add an offset for the point – Arūnas Bedžinskas Nov 13 '19 at 09:46
  • this will cause inaccurate positions when you try to navigate from distant points, not to mention that it will not unless you animate the camera. – desgraci Jun 17 '20 at 14:08
1

Google map v3 beta seems to fix it, using padding now properly move pivotX/pivotY for rotation https://developers.google.com/maps/documentation/android-sdk/v3-client-migration

Samuel Eminet
  • 4,647
  • 2
  • 18
  • 32
0

Accepted answer might be work of "animateCamera". Its makes "shaking of rotations" almost invisible. So more easy will be using

mMap.setPadding(leftPx, topPx, rightPx, bottomPx)

with mMap.animateCamera or twice mMap.moveCamera(in case of changing bearing 1nd moveCamera rotate with center of device, 2nd moveCamera make correct center using padding)

Its kind of hack, while google do not fix rotation considering setPadding

Andrii
  • 11
  • 3