博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
改变视图
阅读量:7021 次
发布时间:2019-06-28

本文共 6323 字,大约阅读时间需要 21 分钟。

地图视图被建模为一个在平面上向下看的照相机。照相机的位置(并且因此地图的渲染)由如下的属性描述:

移动照相机

Maps API允许你改变地图上世界的什么部分是可见的。这通过改变照相机的位置来实现(而不是移动地图)。

当你改变照相机的时候,你可以选择用动画的形式来呈现产生的相机运动。动画将插在当前的相机属性和新的相机属性之间。你也可以控制动画的持续时间。

Note: All programmatic camera movements are calculated against size of the GoogleMap object after first taking into account any padding that has been added to the map. For example, adding 100 pixels of padding to the left edge of your map will shift the center of your map to the right by 50 pixels. More information is available in the  documentation.

为了改变相机的位置,你必须使用一个指定你想要将相机移至何处。Maps API允许你使用CameraUpdateFactory创建许多不同类型的CameraUpdate。下面的选项是可用的:

改变缩放比例

给你提供了一个将缩放比例改变1.0,而同时保持其他属性不变的CameraUpdate。

为你提供了将缩放比例修改为给定值,同时保持所有其他属性不变的CameraUpdate。

为你提供了将缩放比例增加(或减少,如果值为负的话)给定值的一个CameraUpdate。后者将修订给定点在屏幕上的位置,以使它保持在相同位置(纬度/经度),并且它可能改变相机的位置以实现这一点。

改变相机的位置

有两个方便的方法用于普通的位置调整。给你一个改变了相机经纬度,而同时保持所有其他属性的CameraUpdate。给你一个改变了相机经纬度及缩放比例,而同时保持所有其他属性的CameraUpdate。

想要随意的改变相机的位置,则使用,它为你提供一个,其会将相机移至给定的点。一个CameraPosition可以使用CameraPosition()直接获取,或使用new CameraPosition.Builder()通过一个CameraPosition.Builder来获取。

设定边界

移动相机以使感兴趣的整个区域在最大的可能的缩放比例上都可见在有些时候是很有用的。比如,如果你在显示用户当前位置5英里范围内的所有加油站,则你可能想要移动相机以使它们在屏幕上都是可见的。为了做到这一点,首先要计算出你想要在屏幕上使其可见的。然后你可以使用CameraUpdateFactory.newLatLngBounds(LatLngBounds bounds, int padding)获取一个改变相机位置以使给定LatLngBounds在地图内完全适合,同时考虑了指定的填充(以像素为单位)。返回的CameraUpdate将确保给定的边界与地图的边缘之间的间隙(以像素为单位)将至少与指定的填充一样大。注意地图的tilt和bearing将都是0。

private GoogleMap mMap;// Create a LatLngBounds that includes Australia.private LatLngBounds AUSTRALIA = new LatLngBounds(  new LatLng(-44, 113), new LatLng(-10, 154));// Set the camera to the greatest possible zoom level that includes the// boundsmMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AUSTRALIA, 0));

In some cases, you may wish to center your camera within a bounds instead of including the extreme borders. For example, to center the camera on a country while maintaining a constant zoom. In this case, you can use a similar method, by creating a   and using CameraUpdateFactory.newLatLngZoom(LatLng latLng, float zoom)  with the LatLngBounds .getCenter()  method. The getCenter() method will return the geographic center of the LatLngBounds.

private GoogleMap mMap;private LatLngBounds AUSTRALIA = new LatLngBounds(  new LatLng(-44, 113), new LatLng(-10, 154));mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(AUSTRALIA.getCenter(), 10));

An overload of the method,   allows you to specify a width and height in pixels for a rectangle, with the intention that these correspond to the dimensions of the map. The rectangle is positioned such that its center is the same as that of the map's view (so that if the dimensions specified are the same as those of the map's view, then the rectangle coincides with the map's view). The returned CameraUpdate  will move the camera such that the specified LatLngBounds  are centered on screen within the given rectangle at the greatest possible zoom level, taking into account the padding required.

Note: Only use the simpler method  to generate a CameraUpdate if it is going to be used to move the camera after the map has undergone layout. During layout, the API calculates the display boundaries of the map which are needed to correctly project the bounding box. In comparison, you can use the CameraUpdate returned by the more complex method  at any time, even before the map has undergone layout, because the API calculates the display boundaries from the arguments that you pass.

Panning (scrolling)

CameraUpdateFactory.scrollBy(float, float) gives you a CameraUpdate that changes the camera's latitude and longitude such that the map moves by the specified number of pixels. A positive x value causes the camera to move to the right, so that the map appears to have moved to the left. A positive y value causes the camera to move down, so that the map appears to have moved up. Conversely, negative x values cause the camera to move to the left, so that the map appears to have moved right and negative y values cause the camera to move up. The scrolling is relative to the camera's current orientation. For example, if the camera has a bearing of 90 degrees, then east is "up".

更新相机视图

为了给地图应用一个,你可以立即地移动相机或使用动画平滑的移动。为了通过给定的CameraUpdate立即地移动相机,你可以调用GoogleMap.moveCamera(CameraUpdate)。

You can make the user experience more pleasing, especially for short moves, by animating the change. To do this instead of calling  call. The map will move smoothly to the new attributes. The most detailed form of this method, , offers three arguments:

cameraUpdate

   The   describing where to move the camera.

callback

   An object that implements  . This generalized interface for handling tasks defines two methods `onCancel()` and `onFinished()`. For animation, the methods are called in the following circumstances: Invoked if the animation goes to completion without interruption.

Invoked if the animation is interrupted by calling stopAnimation() or starting a new camera movement.

Alternatively, this can also occur if you call .

duration

   Desired duration of the animation, in milliseconds, as an `int`.

The following code snippets illustrate some of the common ways to move the camera using the Maps API.

private static final LatLng SYDNEY = new LatLng(-33.88,151.21);private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1);private GoogleMap map;... // Obtain the map from a MapFragment or MapView.// Move the camera instantly to Sydney with a zoom of 15.map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));// Zoom in, animating the camera.map.animateCamera(CameraUpdateFactory.zoomIn());// Zoom out to zoom level 10, animating with a duration of 2 seconds.map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.CameraPosition cameraPosition = new CameraPosition.Builder()    .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View    .zoom(17)                   // Sets the zoom    .bearing(90)                // Sets the orientation of the camera to east    .tilt(30)                   // Sets the tilt of the camera to 30 degrees    .build();                   // Creates a CameraPosition from the buildermap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Done.

转载于:https://my.oschina.net/wolfcs/blog/180874

你可能感兴趣的文章
leetcode-598-Range Addition II
查看>>
springboot + shiro 验证码与记住登录
查看>>
小猿圈分享HTML5中form如何关闭自动完成功能的方法
查看>>
Carthage 安装与使用
查看>>
详解 Cookie,Session,Token
查看>>
jq 登录正则验证
查看>>
TCP之三次握手和四次挥手
查看>>
【算法学习笔记】70.回文序列 动态规划 SJTU OJ 1066 小M家的牛们
查看>>
phpcms v9 评论的bug.
查看>>
使用Jmeter进行APP接口测试经验总结
查看>>
微信智能硬件应用——微信插座控制
查看>>
有关public接口和友元类的讨论
查看>>
Poj 1050 分类: Translation Mode ...
查看>>
bk.
查看>>
ASP.NET页面间跳转和传递数据(转)
查看>>
使用Coding体验小记
查看>>
bind封装
查看>>
Leetcoder 前序,中序,后序遍历代码
查看>>
c# windows编程控件学习-2
查看>>
EXCEL中R1C1样式引用
查看>>