繁体   English   中英

我想将图标从一个位置点移动到另一点,图标应在Android中将一个位置移动到另一个位置

[英]I want to move icon from one location point to another point where icon should move one location to other in android

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

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

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;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM