标记在Google Map中闪烁

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了标记在Google Map中闪烁相关的知识,希望对你有一定的参考价值。

我已实现基于安全目的的应用。这个应用程式适用于A和B这两个角色。两个使用者都可以追踪彼此的位置。对于用户A,我已经实现了Mapbox,对于用户B我已经实现了Google Map。

对于A,Mapbox提供了默认的实时导航,对于B,我使用了提供用户A位置的信号R。因此,我可以将标记导航到A的特定位置。

我在从SignalR获取位置时遇到了问题。但是标记在闪烁。

我已经使用代码如下移动标记。

private val mMoveCarLocation = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent != null) {
                val handler = Handler()
                //Code to move car along latitude and longitude
                handler.postDelayed(object : Runnable {
                    override fun run() {
                        try {
                            //post again
                            Log.d("tess", "inside run ")
                            val targetLocation = Location(LocationManager.GPS_PROVIDER)

                            val latitude = java.lang.Double.valueOf(
                                intent.getStringExtra(
                                    GlobalVariables.PROVIDE_A_LATITUDE
                                )
                            )
                            val longitude =
                                java.lang.Double.valueOf(intent.getStringExtra(GlobalVariables.PROVIDE_A_LONGITUDE))
                            targetLocation.latitude = latitude
                            targetLocation.longitude = longitude
                            animateMarkerNew(targetLocation)
                            mActivityUserMapBinding!!.txtTime.setText(
                                intent.getStringExtra(
                                    REMAINING_TIME
                                )
                            )
                        } catch (e: java.lang.Exception) {
                            Log.d("tess", "call back removed")
                            //removed callbacks
                            handler.removeCallbacks(this)
                        }
                    }
                }, 3000)
            }
        }
    }

动画标记​​

fun animateMarkerNew(destination: Location) {
        if (viewModel.mMarker != null) {
            val startPosition = viewModel.mMarker.position
            val endPosition = LatLng(destination.latitude, destination.longitude)

            val startRotation = viewModel.mMarker.rotation
            val latLngInterpolator = UserMapViewModel.LatLngInterpolatorNew.LinearFixed()

            val valueAnimator = ValueAnimator.ofFloat(0F, 1F)
            valueAnimator.duration = 3000 // duration 3 second
            valueAnimator.interpolator = LinearInterpolator()
            valueAnimator.addUpdateListener { animation ->
                try {
                    val v = animation.animatedFraction
                    val newPosition =
                        latLngInterpolator.interpolate(v, startPosition, endPosition)
                    viewModel.mMarker.setPosition(newPosition)

                    handler = Handler()
                    handler!!.postDelayed(Runnable {
                        viewModel.googleMap!!.moveCamera(
                            CameraUpdateFactory.newCameraPosition(
                                CameraPosition.Builder()
                                    .target(newPosition)
                                    // .zoom(15.5f)
                                    .zoom(17.5f)
                                    .build()
                            )
                        )

                        viewModel.mMarker.rotation = viewModel.getBearing(
                            startPosition,
                            LatLng(destination.latitude, destination.longitude)
                        )
                    }, 800)
                } catch (ex: Exception) {
                    //I don't care atm..
                }
            }
            valueAnimator.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                }
            })
            valueAnimator.start()
        }
    }
答案
  public static void animateMarker(LocationInfo destination, Marker marker, float bearning) {
    if (marker != null) {
        LatLng startPosition = marker.getPosition();
        LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude());

        float startRotation = marker.getRotation();

        LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.setDuration(1000); // duration 1 second
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override public void onAnimationUpdate(ValueAnimator animation) {
                try {
                    float v = animation.getAnimatedFraction();
                    LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
                    marker.setPosition(newPosition);
//                        marker.setRotation(computeRotation(v, startRotation, bearning));
                } catch (Exception ex) {
                    // I don't care atm..
                }
            }
        });

        valueAnimator.start();
    }
}



 interface LatLngInterpolator {
 LatLng interpolate(float fraction, LatLng a, LatLng b);

class LinearFixed implements LatLngInterpolator {
    @Override
    public LatLng interpolate(float fraction, LatLng a, LatLng b) {
        double lat = (b.latitude - a.latitude) * fraction + a.latitude;
        double lngDelta = b.longitude - a.longitude;
        // Take the shortest path across the 180th meridian.
        if (Math.abs(lngDelta) > 180) {
            lngDelta -= Math.signum(lngDelta) * 360;
        }
        double lng = lngDelta * fraction + a.longitude;
        return new LatLng(lat, lng);
    }
}

}

以上是关于标记在Google Map中闪烁的主要内容,如果未能解决你的问题,请参考以下文章

Ionic 5 Google Maps 标记在缩放时闪烁

长时间打开时,Google Map 在 Android TV 中闪烁

android google map supportmap片段无法在片段中初始化

Google Map API - 移除标记

google-map 片段应用程序在模拟器中崩溃后打开 WebView 活动

InfoWindow中的Android Google地图摘要自动更新