栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

像Uber Android一样在地图上旋转标记并移动动画

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

像Uber Android一样在地图上旋转标记并移动动画

我最近遇到了相同的用例。这是我的解决方案。

首先,我要感谢@VipiN分享了“ The Moving Moving Car Code”。它工作顺利。

第二部分是将汽车标记放置在正确的方向上,并按照转弯方向旋转它。为此,我计算了两个连续点之间的方位角或航向角(即,您从设备/服务器收到的位置更新)。该链接将帮助您了解其背后的数学原理。

以下代码将为您提供两个位置之间的关系:

private double bearingBetweenLocations(LatLng latLng1,LatLng latLng2) {    double PI = 3.14159;    double lat1 = latLng1.latitude * PI / 180;    double long1 = latLng1.longitude * PI / 180;    double lat2 = latLng2.latitude * PI / 180;    double long2 = latLng2.longitude * PI / 180;    double dLon = (long2 - long1);    double y = Math.sin(dLon) * Math.cos(lat2);    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);    double brng = Math.atan2(y, x);    brng = Math.toDegrees(brng);    brng = (brng + 360) % 360;    return brng;}

最后,我们需要将汽车标记旋转上述方法获得的角度。

private void rotateMarker(final Marker marker, final float toRotation) {    if(!isMarkerRotating) {        final Handler handler = new Handler();        final long start = SystemClock.uptimeMillis();        final float startRotation = marker.getRotation();        final long duration = 1000;        final Interpolator interpolator = new LinearInterpolator();        handler.post(new Runnable() { @Override public void run() {     isMarkerRotating = true;     long elapsed = SystemClock.uptimeMillis() - start;     float t = interpolator.getInterpolation((float) elapsed / duration);     float rot = t * toRotation + (1 - t) * startRotation;     marker.setRotation(-rot > 180 ? rot / 2 : rot);     if (t < 1.0) {         // Post again 16ms later.         handler.postDelayed(this, 16);     } else {         isMarkerRotating = false;     } }        });    }}

干杯!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号