I am using Google Map API in an Android app and trying to rotate the map. I have already done it by changing the bearing value but the problem is that it rotates around the center of the screen.
CameraPosition bottomPos = new CameraPosition.Builder()
.bearing(bearing)
.build();
mainMap.animateCamera(CameraUpdateFactory.newCameraPosition(bottomPos));
I want to change its axis of rotation and make it rotate around a point at bottom of screen
Is there any function to change the axis of rotation of the map?
Anyway, you can use workaround like that - move map rotation center point to bottom of screen by creating MapView (MapFragment) twice as wide and as high and using margins align it position to center bottom like on a figure below:
In that case, with default:
CameraPosition bottomPos = new CameraPosition.Builder()
.bearing(bearing)
.build();
mainMap.animateCamera(CameraUpdateFactory.newCameraPosition(bottomPos));
your mainMap will turn around center of bottom of screen.
UPDATE:
Also you can use get current map center screen coordinates, then change y coordinate and get new screen center. Something like that:
...
CameraPosition bottomPos = new CameraPosition.Builder()
.bearing(bearing)
.build();
mainMap.moveCamera(CameraUpdateFactory.newCameraPosition(bottomPos));
LatLng mapCenter = mainMap.getCameraPosition().target;
Projection projection = mainMap.getProjection();
Point centerPoint = projection.toScreenLocation(mapCenter);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
centerPoint.y = centerPoint.y - displayHeight / 2; // move center down
LatLng newCenterPoint = projection.fromScreenLocation(centerPoint);
mainMap.animateCamera(CameraUpdateFactory.newLatLngZoom(newCenterPoint, zoom));
...
Related
The Heatmap Overlay for the Android Google Maps API renders the radius in pixels, which means that by zooming in and out the radius gets bigger and smaller.
I need a heatmap with fixed radius (e.g. in meters instead of pixels) that does not rescale when zooming in and out.
Is there any possibility to do so?
In googlemap api, get scale data. In UI you have the legend which states the map scale. Something like this - (check in google maps bottom right I'm talking about 2000Ft thing below)
You know cm length of the phone screen, you know the pixels in complete width,so
cm(phoneScreen) --> cm/pixel ratio --> pixel radius to cm radius.
you can deduce using some maths I suppose.
As for my opinion you can use setRadius() method to change the radius of heatmaps location in response to changed zoom. Something like this:
int DEFAULT_ZOOM_LEVEL = 10;
int ZOOM_STEP_FOR_RADIUS = 2;
mProvider = new HeatmapTileProvider.Builder()
.data(mList)
.setRadius(DEFAULT_ZOOM_LEVEL)
.build();
mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener({
#Override
void onCameraIdle() {
float newZoom = mMap.getCameraPosition().zoom;
mProvider.setRadius(DEFAULT_ZOOM_LEVEL + newZoom * ZOOM_STEP_FOR_RADIUS)
mOverlay.clearTileCache();
}
});
I get a .png from my server that represents a satellite image and I would put this image like a layer over the OpenStreetMap so I can see the street (under the satellite image) and the aerial photo on the same map.
I use this method
Drawable d = new BitmapDrawable(getResources(), bitmap);
bb = map.getBoundingBox();
Location SO = new Location(LocationManager.GPS_PROVIDER);
SO.setLatitude(bb.getLonWest());
SO.setLongitude(bb.getLatSouth());
Location NO = new Location(LocationManager.GPS_PROVIDER);
NO.setLatitude( bb.getLonWest());
NO.setLongitude(bb.getLatNorth());
Location NE = new Location(LocationManager.GPS_PROVIDER);
NE.setLatitude(bb.getLonEast());
NE.setLongitude(bb.getLatNorth());
float height = SO.distanceTo(NO);
float width = NE.distanceTo(NO);
Projection projection = map.getProjection();
int y = map.getHeight() / 2;
int x = map.getWidth() / 2;
GeoPoint geoPoint = (GeoPoint) projection.fromPixels(x, y);
deleteLayers(GroundOverlay.class);
GroundOverlay myGroundOverlay = new GroundOverlay();
myGroundOverlay.setImage(d);
myGroundOverlay.setPosition(geoPoint);
myGroundOverlay.setDimensions(width*0.94f, height*1.06f);
map.getOverlays().add(myGroundOverlay);
map.invalidate();
the problem is that the image always is stretch. my png cover only a part of my map (and it's ok beacuse our png is this way). When i move in the map the image stretching in all map but and it cover a part of map that it not rappresent.
Also, the image is not in the right place (look the red arrow).
How can i fix it?
Assuming your satellite image is limited to a portion of map you can use osmbonuspack GroundOverlay.
Probably this issue: https://github.com/MKergall/osmbonuspack/issues/273
Look at the hint about corrective coefficients.
Using MapBox in Android, I am trying to find the lower left corner and upper right corner. I cannot find anywhere in their documentation to retrieve this information.
This line of code should accomplish what your trying to do:
LatLngBounds bounds = mapboxMap.getProjection().getVisibleRegion().latLngBounds;
EDIT
Sorry about that, This is a feature upcoming in 4.0. for now you can use this as a work around:
int viewportWidth = mMapView.getWidth();
int viewportHeight = mMapView.getHeight();
LatLng topLeft = mMapView.fromScreenLocation(new PointF(0, 0));
LatLng topRight = mMapView.fromScreenLocation(new PointF(viewportWidth, 0));
LatLng bottomRight = mMapView.fromScreenLocation(new PointF(viewportWidth, viewportHeight));
LatLng bottomLeft = mMapView.fromScreenLocation(new PointF(0, viewportHeight));
Hope this helps!
I working with google maps in Android. I am stuck with adjusting zoom level.
I have one marker in center of map that i set by using mapController.setCenter(current_location);
And I have 10 or more marker showing in map with different color.
Expected Result: current location of person should in center always and I have to adjust a zoom level so that green marker will show in screen( Current location marker in center).
What I tried:
int greenlat = (int) (AppCoordinates.getInstance().latitudelist.get(0) * 1E6);
int greenlong = (int) (AppCoordinates.getInstance().longitudelist.get(0) * 1E6);
int currentlat = (int) (AppCoordinates.getInstance().getCurrentLatitude() * 1E6);
int currentlong = (int) (AppCoordinates.getInstance().getCurrentLongitude() * 1E6);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mapController.zoomToSpan((int) (Math.abs(greenlat - currentlat) * 1), (int)(Math.abs(greenlong - currentlong) * 1));// to have some padding on the edges.
mapController.animateTo(new GeoPoint((greenlat + currentlat), (greenlong + currentlong)/2));
With is I am able to show booth the markers in screen, but I want cureent location to be shown in center of screen.
My idea is to manipulate LatLngBounds since its representing all included markers to be viewed in aligned rectangle on the map. So we just need to recognise all points that make current location will be viewed at the center of map.
Put all markers into the bound.
Move camera to your current location as center point.
Get 4 locations of all corners of the current map and put it together in list of point in the bound.
this is my sample:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
Marker myLocation = mMap.addMarker(new MarkerOptions()
.position(new LatLng(2.6,101.5))
.title("My-Current-Location"));
Marker addMarker1 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(2.6,101))
.title("Hello world-1"));
Marker addMarker2 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(2.7,101))
.title("Hello world-2"));
Marker addMarker3 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(3.7,105))
.title("Hello world-3"));
builder.include(myLocation.getPosition());
builder.include(addMarker1.getPosition());
builder.include(addMarker2.getPosition());
builder.include(addMarker3.getPosition());
//haha enough to use for loop for all your markers
int padding = 100; // offset from edges of the map in pixels
CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
mMap.moveCamera(cameraUpdate);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation.getPosition()));
LatLng nrLatLng = mMap.getProjection().getVisibleRegion().nearRight;
LatLng frLatLng = mMap.getProjection().getVisibleRegion().farRight;
LatLng nlLatLng = mMap.getProjection().getVisibleRegion().nearLeft;
LatLng flLatLng = mMap.getProjection().getVisibleRegion().farLeft;
builder.include(nrLatLng);
builder.include(frLatLng);
builder.include(nlLatLng);
builder.include(flLatLng);
cameraUpdate =CameraUpdateFactory.newLatLngBounds(builder.build(), padding);
mMap.animateCamera(cameraUpdate);
After starting new project, i've faced a problem with zooming maps in google maps v2 in android. I can't zoom map using default controls. The following code shows Camera position changing
CameraPosition currentPlace = new CameraPosition.Builder()
.target(mMarker.getPosition())
.bearing(bearing)
.zoom(14f) //apply zoom here
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(currentPlace));
After this function i cannot zoom map with default controls
The solution to this problem is that we should use markers zoom when we move camera :
CameraPosition currentPlace = new CameraPosition.Builder()
.target(mMarker.getPosition()).bearing(bearing)
.zoom(map.getCameraPosition().zoom)
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(currentPlace));
Try this,
get the location longitude and latitude
static LatLng loc1 = new LatLng(6.9009960, 79.8548823);
//zoom the location
map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc1, 15));