目标是捕获用户在地图中导航的位置的纬度/长度。它的中心有一个标记。
这是我到目前为止所得到的:
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));
}
});
它可以工作,但它会产生数以千计的标记,正如您可能已经猜到的那样。我认为标记应该在移动侦听器之外创建,并且每次相机移动时都会动画到中心。
那么,我该如何实现呢?
答案 0 :(得分:1)
只需保留一个始终指向当前中心的Marker参考。然后,在放置新的时,如有必要,删除旧的:
//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));
}
});
答案 1 :(得分:0)
您只需要修改 在“ mCenterMarker = setPosition(myMap.getCameraPosition()。target)”行中 像这样
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));
}
答案 2 :(得分:-1)
您可以创建一个通用标记变量,只需使用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));
}
然后在其他地方定义mCenterMarker。