简体   繁体   English

移动相机时移动以相机为中心的谷歌地图标记

[英]Move camera-centered google-maps marker as you move the camera around

The goal is to capture the lat/long of where ever the user navigates in the map.目标是捕获用户在地图中导航的位置的纬度/经度。 There is a marker right on the center of it.在它的正中央有一个标记。

Here's what I got so far:这是我到目前为止得到的:

    myMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            CameraPosition test = myMap.getCameraPosition();
            myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).anchor(0.5f, .05f).title("Test"));
            Log.d(TAG, "Map Coordinate: " + String.valueOf(test));
        }
    });

It works, but it creates thousands of markers as you might have guessed.它有效,但正如您可能已经猜到的那样,它会创建数千个标记。 I'm thinking the marker should be created outside the move listener and animated to the center every time the camera moves.我认为标记应该在移动侦听器外部创建,并在每次相机移动时动画到中心。

So, how can I achieve that?那么,我该如何实现呢?

Simply keep one Marker reference that will always point to the current center.只需保留一个始终指向当前中心的标记参考。 Then, when placing a new one, remove the old one if necessary:然后,在放置新的时,如有必要,请移除旧的:

//Instance variable:
Marker mCenterMarker;

myMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
    @Override
    public void onCameraMove() {

        //Remove previous center if it exists
        if (mCenterMarker != null) {
            mCenterMarker.remove();
        }

        CameraPosition test = myMap.getCameraPosition();
        //Assign mCenterMarker reference:
        mCenterMarker = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).anchor(0.5f, .05f).title("Test"));
        Log.d(TAG, "Map Coordinate: " + String.valueOf(test));
    }
});

The goal is to capture the lat/long of where ever the user navigates in the map.目标是捕捉用户在地图中导航的纬度/经度。 There is a marker right on the center of it.在它的中心有一个标记。

Here's what I got so far:这是我到目前为止所得到的:

    myMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            CameraPosition test = myMap.getCameraPosition();
            myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).anchor(0.5f, .05f).title("Test"));
            Log.d(TAG, "Map Coordinate: " + String.valueOf(test));
        }
    });

It works, but it creates thousands of markers as you might have guessed.它有效,但正如您可能已经猜到的那样,它创建了数千个标记。 I'm thinking the marker should be created outside the move listener and animated to the center every time the camera moves.我认为标记应该在移动侦听器之外创建,并在每次相机移动时动画到中心。

So, how can I achieve that?那么,我怎样才能做到这一点?

You could create a universal marker variable, and just change it's location based on the camera using setPosition(myMap.getCameraPosition().target)您可以创建一个通用标记变量,然后使用setPosition(myMap.getCameraPosition().target)根据相机更改它的位置

//Instance variable:
Marker mCenterMarker;

myMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {

    //Remove previous center if it exists

    CameraPosition test = myMap.getCameraPosition();
    //Assign mCenterMarker reference:
    mCenterMarker = setPosition(myMap.getCameraPosition().target)
    Log.d(TAG, "Map Coordinate: " + String.valueOf(test));
}

then define mCenterMarker somewhere else.然后在其他地方定义 mCenterMarker 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM