【问题标题】:Display message at a specific location in Google Maps Activity?在 Google 地图活动中的特定位置显示消息?
【发布时间】:2017-11-27 00:07:27
【问题描述】:

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

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

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    您可以将任何View 放置在MapFragment(或MapView)上并使用Animations 对其进行动画处理:动画大小、位置、透明度等,使用各种interpolators 并将多个动画合并到一个通过AnimatorSet。要获取特定LatLng 坐标的View 的屏幕位置,您可以使用Projection.toScreenLocation() 方法。

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

    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>
    

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

    结果:

    【讨论】:

    • 这很好,我已经知道这种可能性。但是,我想要一个更精致的版本,用于专业的公司应用程序。这就是我最初要求图书馆的原因。拥有一个纯 TextView 是不够的。
    • @user8618899 对于popUpView() 方法,您可以使用任何视图(或布局)而不是TextView
    • 什么是最赏心悦目的选项?
    • @user8618899 这取决于。恕我直言,我会使用自定义视图进行复杂布局或使用TextViewhtml(如this)或可扩展的丰富格式(如that)。甚至将ImageView 与以编程方式生成的位图一起使用...
    【解决方案2】:

    我建议你只使用标记。 作为标记,它自己提供了一些功能,如动画等。

    您还可以以不同的方式自定义标记以显示文本而不是图像,您还可以使用自定义文本,如 html 文本显示更多由 html 和 css 支持的修饰文本。

    这是一个简单而出色的解决方案,因为我在我的项目中也使用了相同的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2019-07-01
      • 2017-01-10
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多