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

时间:2016-12-22 06:59:40

标签: java android api google-maps

我正在开发一款汽车定位应用程序。我想将图标移动到android中的其他位置我已经尝试删除旧地图图标并清除所有图标并生成新图标。它工作得很好。但它看起来并不好。我想将图标从一个位置移动到Ola app这样的新位置,我正在使用汽车图标,所以我想在将位置更改为90度时转动图标。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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