【问题标题】:Google maps - camera position with marker at bottom谷歌地图 - 底部有标记的相机位置
【发布时间】:2016-06-03 15:18:59
【问题描述】:

如果您查看谷歌导航,它始终将驱动程序标记保持在靠近底部的位置,当您移动相机时,它会提供将其重置回底部。我想知道如何在给定标记的情况下实现相同的目标。

There was a similar question before,但提供的答案不认为地图倾斜导致投影错误。

【问题讨论】:

  • 发布您的 layout.xml 文件。
  • @ChiragSavsani 为什么?这与布局无关,假设支持地图片段已加载到框架布局中。
  • 试试this。对我来说效果很好。

标签: android google-maps google-maps-android-api-2


【解决方案1】:

当前位置标记在移动时始终位于屏幕底部。为此,我们必须设置

map.setPadding(0,320,0,0);

所以,我们将 pdding top 设置为地图 320,然后它会从屏幕顶部占用一些空间。 在你的最终代码中是这样的

 CameraPosition cameraPosition = new CameraPosition.Builder()
                                .target(newLatLng)                         
                                .zoom(18)                          
                                .bearing(0)            
                                .tilt(0)          
                                .build();
               map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
               map.setPadding(0,320,0,0);

【讨论】:

  • 你成就了我的一天
【解决方案2】:

尼玛,有不同的方法可以通过调整相机位置的值来实现这种行为。

例如,您有 2 个可用的地理位置 latlng 信息,UserLocation 和 DestinationLocation 找到该位置的中点并将相机目标设置在该位置。然后,您可以通过指定方位值将相机移动到缩放级别,该缩放级别覆盖地理定位,并在顶部和底部进行适当的填充。

    //First build the bounds using the builder
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    LatLngBounds bounds;
    builder.include(userLocation);
    builder.include(destinationLocation);
    bounds = builder.build();
    // define value for padding 
    int padding =20;
    //This cameraupdate will zoom the map to a level where both location visible on map and also set the padding on four side.
    CameraUpdate cu =  CameraUpdateFactory.newLatLngBounds(bounds,padding);
    mMap.moveCamera(cu);

    // now lets make the map rotate based on user location
    // for that we will add the bearing to the camera position.
    //convert latlng to Location object
    Location startLocation = new Location("startingPoint");
    startLocation.setLatitude(userLocation.latitude);
    startLocation.setLongitude(userLocation.longitude);

    Location endLocation = new Location("endingPoint");
    endLocation.setLatitude(destinationLocation.latitude);
    endLocation.setLongitude(destinationLocation.longitude);

    //get the bearing which will help in rotating the map.
    float targetBearing = startLocation.bearingTo(endLocation);               

    //Now set this values in cameraposition
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(bounds.getCenter())
            .zoom(mMap.getCameraPosition().zoom)
            .bearing(targetBearing)
            .build();
    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

【讨论】:

  • 这不能回答我的问题。正如您在屏幕截图中看到的那样,“endLocation”不在视图中,因此简单的 LatLngBounds 不是我想要的
  • 您可以随时设置任何其他位置而不是目的地位置,考虑您的图像在这里您可以使用下一个转弯或下一个 100 米地理点的位置。解决方案是设置适当的缩放级别并设置正确的方位,否则尝试找到您首先在用户位置上设置相机目标的解决方案,这会将地图焦点放在用户标记上并将其置于屏幕中心,然后移动相机再次到地图屏幕的上半部分 xy 到地图的投影,通过设置该目标,它将移动底部的标记。
  • stackoverflow.com/questions/16764002/… 此链接将帮助您如何设置相机焦点,使标记出现在屏幕底部。
  • 该链接是我最初在我的问题中发布的,请阅读我的帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 2019-10-11
  • 2016-09-07
  • 2014-03-22
  • 2017-08-08
  • 1970-01-01
相关资源
最近更新 更多