【问题标题】:Android mapview - how to get the center of a selected part of the map?Android mapview - 如何获取地图选定部分的中心?
【发布时间】:2015-10-08 08:10:09
【问题描述】:

我有一个地图视图,它填满了屏幕。在地图顶部,我有一个“显示和隐藏区域”,其中包含一些细节,如下所示:

在这种情况下,我想在地图上的特定位置(纬度、经度)上显示一个标记。 我的问题是让标记始终可见,所以当显示“显示和隐藏区域”时,标记应该在底部地图的可见区域(标记在红色框中)。现在标记显示在“显示和隐藏区域”后面(在地图的中心)。 通过执行以下操作,我只能获得以像素(高度)为单位的地图视图区域:(height_of_map)-(height_of_details_area)。但是当我使用 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);
}

我希望任何人都能理解这个问题并知道如何解决它。

(是的,当我显示/隐藏详细信息区域时,我考虑过调整地图的大小,但我发现这个解决方案有点“丑陋”)

【问题讨论】:

    标签: java android google-maps android-mapview


    【解决方案1】:

    我自己找到了方法。我希望这对其他人有用:

    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 变量,是问题中红框区域高度的一半。

    【讨论】:

      【解决方案2】:

      试试这个

      map.getCameraPosition().target
      

      在map api中,target是'相机指向的位置'。

      【讨论】:

        【解决方案3】:

        首先,您需要获取地图容器的引用,并通过将宽度和高度除以 2 来计算中心点。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-19
          相关资源
          最近更新 更多