【问题标题】:Center fixed marker in android v2 mapviewandroid v2 mapview中的中心固定标记
【发布时间】:2014-11-19 11:05:14
【问题描述】:

我想在拖动地图时在mapview的中心点设置固定标记。它已在android Map V1 google mapview完成。但现在它已被弃用。现在我的问题是,在 android Map V2 google mapview 中是否有可能?(我试过了。但地图没有显示)

【问题讨论】:

  • 你尝试过使用 moveCamera() 或 animateCamera() 吗?
  • 是的。我的问题是标记在拖动地图时也在移动
  • 拖动地图时想在mapView的中心设置固定标记

标签: android android-layout android-fragments android-activity


【解决方案1】:

所以你想在地图中心放置一个十字准线,对吧?

我没有使用过 MapView。但是我使用了 Map Fragment,我在地图上实现修复标记的方式是使用 ImageView,所以你最终会得到如下所示的内容:

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

    <fragment
        android:id="@+id/fragment1"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/cross_hairs" />

</RelativeLayout>

您可以将 MapFragment 与 MapView 交换。

在地图上放置标记效率低下,因为您需要在用户平移地图时不断更新标记。

希望对你有帮助!

【讨论】:

  • 更好的方法。非常感谢。
  • 这种情况下怎么给imageview打阴影,阴影必须落在map上
【解决方案2】:

我打赌您使用的是 getMapCenter(),根据适用于 Android 的 Google 地图 v2,它不再可用。不过不用担心,就用这个吧:

GoogleMap.getCameraPosition().target

它将返回一个LatLng 对象,它基本上代表了地图的中心。然后,您可以通过将OnCameraChangedListener 分配给您的GoogleMap 来使用它在每次发生拖动事件时将标记重新定位到中心。

yourGMapInstance.setOnCameraChangeListener(new OnCameraChangedListener() {
    @Override
    public void onCameraChange (CameraPosition position) {

        // Get the center of the Map.
        LatLng centerOfMap = yourGMapInstance.getCameraPosition().target;

        // Update your Marker's position to the center of the Map.
        yourMarkerInstance.setPosition(centerOfMap);
    }
});

那里。我希望这会有所帮助!

【讨论】:

  • 现在,GoogleMap.OnCameraChangeListener() 已被弃用。而不是使用以下方法 GoogleMap.OnCameraMoveStartedListener, GoogleMap.OnCameraMoveListener, GoogleMap.OnCameraIdleListener
【解决方案3】:

请注意,setOnCameraChangeListener() 已被弃用,因此您可以使用新的摄像头侦听器(OnCameraIdleListenerOnCameraMoveListenerOnCameraMoveStartedListenerOnCameraMoveCanceledListener),例如:

map.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
    @Override
    public void onCameraMove() {
        // Get the center of the map
        LatLng center = map.getCameraPosition().target;
        ......
    }
});

欲了解更多信息,请阅读:https://developers.google.com/maps/documentation/android-api/events

【讨论】:

    【解决方案4】:

    只需转到 XML 文件:在地图片段中添加图像视图并将重力设置为 center。然后转到 Java 文件并将以下 java 代码添加到您的 onMayReady 函数中:

     <fragment
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/linearLayout2" />
        
                <ImageView
                    android:id="@+id/imageView_map_marker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginBottom="17dp"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:layout_gravity="center"
                    android:tooltipText="Select Location"
                    app:srcCompat="@drawable/ic_baseline_location_red" />
    
    
        mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
                     @Override
                     public void onCameraMove() {
                         mMap.clear();
                         binding.imageViewMapMarker.setVisibility(View.VISIBLE);
            
                     }
                 });
            
                 mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
                     @Override
                     public void onCameraIdle() {
                         binding.imageViewMapMarker.setVisibility(View.GONE);
                         selectedLatitude=mMap.getCameraPosition().target.latitude;
                         selectedLongitude=mMap.getCameraPosition().target.longitude;
                         MarkerOptions marker = new MarkerOptions().position(mMap.getCameraPosition().target).title("")
                                 .icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.ic_baseline_location_red));
                         mMap.addMarker(marker);
            
                     }
                 });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 2013-06-14
      相关资源
      最近更新 更多