Google地图 - 底部带有标记的摄像头位置 [英] Google maps - camera position with marker at bottom

查看:632
本文介绍了Google地图 - 底部带有标记的摄像头位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您查看Google导航,它始终将驾驶员标记保持在底部,当您移动相机时,它会将其重置为底部。我想知道如何用一个标记来实现同样的事情。



解决方案

尼玛,通过调整相机位置中的值来实现此行为的不同方法。



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

  //首先使用构建器构建边界
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds界限;
builder.include(userLocation);
builder.include(destinationLocation);
bounds = builder.build();
//定义填充值
int padding = 20;
//此摄像机更新将缩放地图到一个级别,其中两个位置都可以在地图上显示,也可以在四边设置填充。
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding);
mMap.moveCamera(cu);

//现在让地图根据用户位置
//旋转,因为我们会将方位添加到相机位置。
//将latlng转换为位置对象
位置startLocation = new Location(startingPoint);
startLocation.setLatitude(userLocation.latitude);
startLocation.setLongitude(userLocation.longitude);

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

//获得有助于旋转地图的方位。
float targetBearing = startLocation.bearingTo(endLocation);

//现在在cameraposition中设置这个值
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(bounds.getCenter())
.zoom(mMap .getCameraPosition()。zoom)
.bearing(targetBearing)
.build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


If you look at google navigation, it always keeps the driver marker close to bottom and when you move the camera it offers to reset it back to bottom. I'm wondering how to achieve the same thing given a marker.

There was a similar question before, but the offered answers do not consider the map to be tilted which results in wrong projection.

解决方案

Nima, there are different way to achieve this behaviour by tweaking values in camera positions.

For instance you have 2 geo location latlng information available with you, UserLocation and DestinationLocation find the midpoint of this location and set the camera target at it. And then you can move the camera to zoom level which cover both geolocation with proper padding of top and bottom by specifying the bearing value.

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

这篇关于Google地图 - 底部带有标记的摄像头位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆