Langkiller
Langkiller

Reputation: 3457

Android mapview - how to get the center of a selected part of the map?

I have a mapview, which fills the screen. On top of the map I have a "show and hide area" with some details like so:

enter image description here

In this situation I want to show a marker on the map on a specific location (latitude, longitude). My problem is to make the marker visible at all times, so when the "show and hide area" is shown, the marker should be in the visible area of the map in the bottom (marked in the red box). Right now the marker is shown behind the "show and hide area" (in the center of the map). I can get only the area of the mapview in pixels (heigth) by doing: (height_of_map) - (height_of_details_area). But that doesn't help me a lot when I use animateCamera method, as this takes in latitude and longitude..

Here is my code for setting the marker and animating the camera to its position:

    private void setMarkerOnMap(double latitude, double longitude) {
    if (marker == null) {
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(12.0f);
        map.animateCamera(zoom);
        marker = map.addMarker(new MarkerOptions().position((new LatLng(latitude, longitude))));
        marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.pin_blue));
    } else {
        marker.setPosition(new LatLng(latitude, longitude));
    }

    CameraUpdate center = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f);
    map.animateCamera(center);
}

I hope anyone can understand the problem and know how to fix it.

(And yes I have thought about resizing the map when I show/hide the details area, but I find this solution to be a bit "ugly")

Upvotes: 0

Views: 1571

Answers (3)

KishuDroid
KishuDroid

Reputation: 5451

At very first step you need to get referance of your map container, and calculate center point by dividing 2 width and height.

 View containerView=findViewById(R.id.mapContainer);        
 LatLng centerPoint=   this.map.getProjection().fromScreenLocation(new Point(((int)containerView.getWidth/2),((int)(containerView.getHeight/2)));

Upvotes: 0

Langkiller
Langkiller

Reputation: 3457

I found a way myself. I hope that this could be useful for someone else:

private void setMarkerOnMap(double latitude, double longitude) {

        if (marker == null) {
            marker = map.addMarker(new MarkerOptions().position((new LatLng(latitude, longitude))));
            marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.pin_blue));
        } else {
            marker.setPosition(new LatLng(latitude, longitude));
        }

        Projection projection = map.getProjection();
        LatLng markerLatLng = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);

        Point markerScreenPosition = projection.toScreenLocation(markerLatLng);
        Point pointHalfScreenAbove = new Point(markerScreenPosition.x, (markerScreenPosition.y - (mapViewHeightFull / 2)) + mapViewHeightHalfPart);
        LatLng aboveMarkerLatLng = projection.fromScreenLocation(pointHalfScreenAbove);

        CameraUpdate camPosition = CameraUpdateFactory.newLatLngZoom(aboveMarkerLatLng, defaultZoom);

        map.animateCamera(camPosition);
    }

The mapViewHeightHalfPart variable, is half of the height of the area marked with red border in the question.

Upvotes: 2

DGN
DGN

Reputation: 702

Try this

map.getCameraPosition().target

In map api, target is 'The location that the camera is pointing at'.

Upvotes: 1

Related Questions