Display message at a specific location in Google Maps Activity?
如何在 Android 上的 Google 地图活动中的特定位置显示消息而不添加标记?我只想在特定坐标处显示一个包含消息的小弹出动画。
是否有任何 GitHub 库可以执行此特定任务?
您可以将任何
例如,对于地图上带有"弹出"动画的"Hello World"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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(); } } |
结果:
我建议你只用记号笔。
作为标记,它自己提供了一些功能,如动画等。
您还可以以不同的方式自定义标记以显示文本而不是图像,您还可以使用自定义文本,如 html 文本显示更多由 html 和 css 支持的修饰文本。
这是一个简单而出色的解决方案,因为我在我的项目中也使用了相同的解决方案。