2015-10-08 51 views
0

我有一個mapview,它填滿了屏幕。在地圖之上我有一個「顯示和隱藏區」,象這樣一些細節:Android mapview - 如何獲取地圖選定部分的中心?

enter image description here

在這種情況下,我想說明的特定位置的地圖上的標記(緯度,經度) 。 我的問題是讓標記在任何時候都可見,所以當顯示「顯示和隱藏區域」時,標記應該位於底部的可見區域(在紅色框中標記)。現在標記顯示在「顯示和隱藏區域」後面(位於地圖的中心)。 我可以通過做:(height_of_map) - (height_of_details_area)以像素爲單位獲取mapview的區域(heigth)。但是,這並不能幫助我很多,當我使用animateCamera方法,因爲這需要在經度和緯度..

這裏是我的設置標記,攝像機動畫到它的位置代碼:

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); 
} 

我希望任何人都能理解問題並知道如何解決問題。

(是的,我曾經想過調整地圖,當我顯示/隱藏細節方面,但我覺得這個解決方案是有點「醜」)

回答

0

試試這個

map.getCameraPosition().target 

在地圖API,目標是'相機指向的位置'。

0

我自己找到了一種方法。我希望,這可能是對別人有用:

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); 
    } 

的mapViewHeightHalfPart變量,是標有問題的紅色邊框的區域的高度的一半。

0

在第一步中,您需要獲取地圖容器的相關信息,並通過除以2的寬度和高度來計算中心點。

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