【问题标题】:getting the location of a static marker when the map is scrolled滚动地图时获取静态标记的位置
【发布时间】:2018-05-18 13:53:35
【问题描述】:

我正在尝试创建一个应用程序,其中地图中有一个静态标记(ImageView)。最初使用 Projection 类,纬度和经度为 0.0,因为标记放置在 xml 地图的中心。当用户滚动到地图中的某个地方以使其显示在静态标记下方时,我想获取该地方的纬度和经度,就像 uber、ola 等。

map.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:layout_constraintBottom_toBottomOf="parent"
        map:layout_constraintEnd_toEndOf="parent"
        map:layout_constraintHorizontal_bias="1.0"
        map:layout_constraintStart_toStartOf="parent"
        map:layout_constraintTop_toTopOf="parent"
        map:layout_constraintVertical_bias="0.0"
        tools:context=".MapsActivity" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        map:layout_constraintBottom_toBottomOf="parent"
        map:layout_constraintEnd_toEndOf="parent"
        map:layout_constraintStart_toStartOf="parent"
        map:layout_constraintTop_toTopOf="parent">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/marker"
            android:background="@drawable/gps"
            map:layout_constraintBottom_toBottomOf="parent"
            map:layout_constraintEnd_toEndOf="parent"
            map:layout_constraintStart_toStartOf="parent"
            map:layout_constraintTop_toTopOf="parent" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        map:layout_constraintBottom_toBottomOf="parent"
        map:layout_constraintEnd_toEndOf="parent"
        map:layout_constraintHorizontal_bias="0.0"
        map:layout_constraintStart_toStartOf="parent"
        map:layout_constraintTop_toTopOf="parent"
        map:layout_constraintVertical_bias="0.035">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/edttxt_bg"
            map:layout_constraintBottom_toBottomOf="parent"
            map:layout_constraintEnd_toEndOf="parent"
            map:layout_constraintHorizontal_bias="0.0"
            map:layout_constraintStart_toStartOf="parent"
            map:layout_constraintTop_toTopOf="parent"
            map:layout_constraintVertical_bias="0.035" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

MapActivity.java:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    ImageView marker;
    Projection projection;
    float x,y;int outlocation[];LatLng location;
    Point point;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        marker=findViewById(R.id.marker);
        outlocation=new int[2];

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.getUiSettings().setMyLocationButtonEnabled(true);
        projection=mMap.getProjection();
        marker.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                marker.getLocationOnScreen(outlocation);
                Log.e("MarkerX",Integer.toString(outlocation[0]));
                Log.e("MarkerY",Integer.toString(outlocation[1]));
                location=projection.fromScreenLocation(new Point(outlocation[0],outlocation[1]));
                Log.e("Lat",Double.toString(location.latitude));
                Log.e("Longt",Double.toString(location.longitude));
            }
        });
        mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                marker.getLocationOnScreen(outlocation);
                Log.e("MarkerX",Integer.toString(outlocation[0]));
                Log.e("MarkerY",Integer.toString(outlocation[1]));
                location=projection.fromScreenLocation(new Point(outlocation[0],outlocation[1]));


            }
        });
    }
}

【问题讨论】:

    标签: android dictionary map-projections


    【解决方案1】:

    您的 xml 看起来不错,但要解决您的问题,您可以或多或少地将 OnCameraIdleListener 添加到您的地图中:

    mMap.setOnCameraIdleListener(object : GoogleMap.OnCameraIdleListener() {
        fun onCameraIdle() {
            val position = mMap.getCameraPosition().target
        }
    })
    

    Google Map.getCameraPosition().target是地图的中心,所以你可以把你的静态imageView保留在地图上模拟做一个标记,让用户可以随意移动地图

    问候

    【讨论】:

    • 是的,我的朋友...我也刚得到它..mMap.getCameraPosition();.这也是正确的吗?
    • 是的,你的 mMap 变量等于我的 mGoogleMap,类似于 mMap.getCameraPosition().target
    猜你喜欢
    • 2013-08-07
    • 2019-09-23
    • 2017-12-03
    • 2017-09-16
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多