Change Google Map Offset Center - android

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

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 :

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

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

Related

Force animateCamera to complete animation

I'm trying to force the camera to animate to a zoom level and coordinate that describes as precisely as possible the area occupied by a list of LatLng coordinates.
So far, I have the following:
private void focusListOfCoords(Collection<LatLng> coords) {
LatLngBounds.Builder coordsBoundsBuilder = new LatLngBounds.Builder();
for (LatLng coord : coords) {
coordsBoundsBuilder.include(coord);
}
LatLngBounds coordsBounds = coordsBoundsBuilder.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(coordsBounds, 50);
mMap.animateCamera(cameraUpdate);
}
The code above, when zoomed all the way out on the whole world simply pans the view slightly to center on the center point of the bounds. It does not zoom in.
If I replace mMap.animateCamera(cameraUpdate) with mMap.moveCamera(cameraUpdate), the projection changes the zoom level (too far in every case, but that's beside the point) and centers as expected.
After trying to troubleshoot this, I noticed that if I implement the CancelableCallback for the animateCamera method, I can see that the animation is being cancelled.
Is there a way to force the animation to finish? If not, how can I hunt down the cause of the animation being cancelled?
Edit: The solution can be found in this question: Android- animateCamera with CameraUpdateFactory.newCameraPosition does NOT zoom
To summarize, my listener method that was calling the animation was returning false which cancelled the animation.
You can try to use a different approach to your implementation.
One is passing CameraUpdateFactory.newLatLngZoom(latlng, zoom) to animateCamera. You can specify the zoom level using this. More info on that here.
Or you can build your CameraView first before passing it to animateCamera. You can check out the docs here.
Not really sure why or how it is trigerring onCancel(). Maybe something is being fired right after your animateCamera call. Try to use the duration parameter to alter the speed of the animation and check for changes.

Cant Zoom Desired Level In Google Maps To Fit 2 Directions In Screen

(I would want to put screenshots put my reputation is low to put them...)
Good Day.I Have two directions(markers) on google maps api(android) between which i want camera to be center,and camera being centered there.But problem is that one marker comes to one edge of screen and another marker to another edge of screen!I don't want it to be like that,i want just to zoom specified level so markers will be center in google maps view and won't be place on edges of screen.I tried to put even 0 zoom level in camera update factory of google maps,but nothing happens at all.Here is my code how do i achieve that
} finally {
com.google.android.gms.maps.model.LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(new LatLng(Double.parseDouble(tolat), Double.parseDouble(tolong)));
boundsBuilder.include(new LatLng(Double.parseDouble(fromlat), Double.parseDouble(fromlong)));
final LatLngBounds bounds = boundsBuilder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));
}
you say you tried to put 0 as the zoom level. You mean this call CameraUpdateFactory.newLatLngBounds(bounds, 0) right?
The second parameter is the padding not the zoom level. Set it up (example to 10) and you have a padding to the outer edges.
https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory
I hope it helps.

CCTMXTiledMap rendered in incorrect position - iOS, Android and Win32

I am using CCTMXTiledMap on cocos2dx-2.2, I created and added the tiled map like this:
// TileMap
CCTMXTiledMap *m_pTileMap = CCTMXTiledMap::create("tilesets/my-isometric-small.tmx");
float fPosX = m_pTileMap->getPositionX();
float fPosY = m_pTileMap->getPositionY();
CCLOG( "TileMapPos: %f, %f", fPosX, fPosY );
this->addChild(m_pTileMap);
The tiled map are created and rendered successfully, but out of position. I use CCTMXTiledMap::getPosition, CCTMXLayer::positionAt, and also examine the CCSprite that I get from CCTMXLayer::tileAt... all of them are returning the correct value based on cocos2d screen coordinate { (0, 0) from bottom left and increasing upward and rightward } However, when viewed on the screen, there is always a slight offset and I can't get where it come from. All the m_obOffsetPosition are confirmed to be zero...
By correct value, I mean the tiles are positioned in the pink area (I getPosition from each of the tile, create CCSprite for each, setPosition of each tile and add it to the screen... They show up in the pink area)
Image supposed to be positioned at shady pink boxes, but instead positioned in the blue area (the entire blue sea is the whole map)
Any ideas are highly appreciated... Thanks!!
After wasting days trying to dissect tilemap_parallax_nodes in cocos2d-x, finally I figured out the culprit... it is the Layer Property cc_vertexz that cause it to be rendered off position. I haven't got the time to figure out how and why it works that way and since I'm not going to use it anyway (I just need flat, single layer, thus no need z order etc), so I just remove that property from all of my Layers and the problem is gone..
Hope it helps someone... Thanks!

android change bounds of google map

I am working on a project in android which requires me to change the bounds of a google map(from bottom only). Currently I am trying by adding padding at the bottom, but this process is slow and the screen also flickers.
Is there any better way to do this?
I would like the user to be able to drag the bottom of the map to change the bounds.
I see a lot of AJAX/JS resouces, but this is a native component.
Best
I think it will be a huge amount of work to let the user actually drag the bottom of your map view. what is much easier and faster to do is to create to 2 map fragments of your map and on click of the map change the fragment back and foreword between the two.
this way the user can change the size of the map the he works with.
You can zoom map to added points.
Here is some pseudo code:
LatLngBounds.Builder bc = new LatLngBounds.Builder();
for (LatLng item : points) {
bc.include(item);
}
LatLngBounds bounds = bc.build();
#SuppressWarnings("deprecation")
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight(), 50);
mMap.moveCamera(cu);
Bounds will be changed to added points. Help It hope

How to find the distance betweeen two points in android

I have drawn few concentric circles and added a gesture listener too on the same view. I have drawn a point where i am placing the marker and tapping the button below. Once i tap the button, i will be getting the (x,y) co ordinates of the position where i place the maker and clicked the button. now i have to find out the distance between the center(x,y) of the view and marking that i have made earlier. I tried with distance calculation form ula, but still i am not sure i am getting the right solution. This is because, for example , when i mark a point at the top portion of the circle and made a mark at the bottom portion of the circle, its giving a different value. Since all are circles, from which ever part of the circle i mark, it has to give me the same distance . am i right ?
I tried with lot of ideas, still nothing work out well.
I have attached the screen shot of that page too, Please take a look at it
Any help is Appreciable.
Thanks.
How are you using the distance formula?
It should just be something like this
Math.sqrt(Math.pow(clickX - centerX, 2) + Math.pow(clickY - centerY, 2));
You can use this formula,
double d = Math.sqrt( (x2-=x1)*x2 + (y2-=y1)*y2 );
Refer this question

Categories

Resources