Issue
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:
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")
Solution
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.
Answered By - Langkiller
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.