在Google Maps Activity中的特定位置显示消息?

1 java android google-maps

如何在不添加标记的情况下在Android上的Google Maps Activity中的特定位置显示消息?我只想显示一个小的弹出动画,其中包含特定坐标处的消息。

是否有可以执行此特定任务的GitHub库?

And*_*nko 5

您可以根据需要使用动画设置任何View上方MapFragment(或MapView)的动画:设置大小,位置,透明度等的动画,使用各种插值器并将多个动画通过合并为一个AnimatorSet。要获取View特定LatLng坐标的屏幕位置,您可以使用Projection.toScreenLocation()方法。

例如,对于TextView在地图上带有“弹出”动画的“ Hello World” ,您可以使用如下代码:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="activities.MainActivity">

    <fragment class="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <TextView
        android:id="@+id/popup_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:visibility="invisible"
        android:text="Hello World!"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private SupportMapFragment mMapSupportedFragment;

    private TextView mPopUpTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPopUpTextView = (TextView) findViewById(R.id.popup_view);

        mMapSupportedFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
        mMapSupportedFragment.getMapAsync(MainActivity.this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                // get screen position for latLng
                Projection projection = mGoogleMap.getProjection();
                Point origin = projection.toScreenLocation(latLng);

                // popup TextView
                popUpView(origin, mPopUpTextView, 500);
            }
        });
    }

    public void popUpView(final Point origin, final View view, long duration){
        view.setX(origin.x);
        view.setY(origin.y);

        view.setVisibility(View.VISIBLE);

        ValueAnimator sizeAnimation = ValueAnimator.ofInt(0, view.getMeasuredHeight());
        sizeAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        sizeAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = val;
                view.setLayoutParams(layoutParams);
            }
        });
        sizeAnimation.setDuration(duration);

        ValueAnimator positionAnimation = ValueAnimator.ofInt(0, view.getMeasuredHeight());
        positionAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        positionAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                view.setY(origin.y - val);
            }
        });
        positionAnimation.setDuration(duration);

        AnimatorSet popUpSet = new AnimatorSet();
        popUpSet.play(sizeAnimation).with(positionAnimation);
        popUpSet.start();

    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

弹出消息