【问题标题】:I want to move icon from one location point to another point where icon should move one location to other in android我想将图标从一个位置点移动到另一个点,图标应该在android中将一个位置移动到另一个位置
【发布时间】:2017-05-07 17:48:57
【问题描述】:

我正在开发一个汽车跟踪应用程序。我想将图标移动到android 中的另一个位置我已经尝试删除旧地图图标并清除所有图标并生成新图标。它运行良好。但它看起来并不好。我想将图标从一个位置移动到一个新位置,例如Ola app,并且我正在使用汽车图标,所以我想在将位置更改为 90 度时转动图标。

【问题讨论】:

    标签: java android api google-maps


    【解决方案1】:
    private double bearingBetweenLocations(LatLng latLng1, LatLng latLng2) {
            final double PI = 3.14159;
            final double lat1 = latLng1.latitude * PI / 180;
            final double long1 = latLng1.longitude * PI / 180;
            final double lat2 = latLng2.latitude * PI / 180;
            final double long2 = latLng2.longitude * PI / 180;
    
            final double dLon = (long2 - long1);
    
            final double y = Math.sin(dLon) * Math.cos(lat2);
            final 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;
        }
    

    计算旋转度数。然后onLocationChanged()再次设置图像:

    @Override
        public void onLocationChanged(Location location) {
            if (location == null)
                return;
            if (mMap != null) {
                if (mPositionMarker != null && mPositionMarker.isVisible()) {
                    mPositionMarker.remove();
                }
                newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
                if (oldLatLng != null) {
                    mPositionMarker = mMap.addMarker(new MarkerOptions()
                            .flat(true)
                            .anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.ic_cab_top)).rotation((float) bearingBetweenLocations(newLatLng, oldLatLng))
                            .position(new LatLng(location.getLatitude(), location
                                    .getLongitude())));
                    animateMarker(mPositionMarker, location); // Helper method for smooth animation
                    mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
                            .getLatitude(), location.getLongitude())));
                }
                oldLatLng = newLatLng;
            }
    

    【讨论】:

    • 感谢您的代码,但您能否说明一下图标将如何移动
    【解决方案2】:

    我假设您使用 Google 地图标记来显示位置。如果是这样,则无需删除标记。而是将标记对象保存在变量中,并在位置更改时更新变量。例如; marker.setPosition();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      相关资源
      最近更新 更多