简体   繁体   English

Google Map API V2 - 如何在用户滚动地图时将标记保留在屏幕中央?

[英]Google Map API V2 - How do I keep a marker in the center of the screen while user is scrolling the map?

Google Map API V2 - How do I keep a marker in the center of the screen while user is scrolling the map ? Google Map API V2 - 如何在用户滚动地图时将标记保留在屏幕中央?

My purpose is to let user choose a location. 我的目的是让用户选择一个位置。 I use the following code to add a marker (it's from the com.example.mapdemo in Android SDK) 我使用以下代码添加标记(它来自Android SDK中的com.example.mapdemo

mMap.addMarker(new MarkerOptions()
    .position(new LatLng(0, 0))
    .title("Marker"));

How do I keep the marker in the center of the screen and then retrieve it's location? 如何将标记保留在屏幕中央,然后检索它的位置?

I do not think that you can actually keep a marker in the middle of the screen and float it easily. 我不认为你可以在屏幕中间保留一个标记并轻松浮动它。 My suggestion is to trick your user. 我的建议是欺骗你的用户。 Add your marker image onto the map like a button. 像标记按钮一样将标记图像添加到地图上。 you can place it in the middle of the screen using the xml layout. 您可以使用xml布局将其放在屏幕中间。 Then when your user selects a location just retrieve the gps coordinates from the middle of the screen. 然后,当您的用户选择一个位置时,只需从屏幕中间检索gps坐标。

On a side note you could also just make your marker draggable Then the user could drag it around the map. 在旁注上你也可以让你的标记可拖动然后用户可以将它拖动到地图上。

mMap.addMarker(new MarkerOptions().position(coordinate)
                    .title("Your Title")
                    .snippet("Please move the marker if needed.")
                    .draggable(true)); 

How about? 怎么样?

mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
        public void onCameraChange(CameraPosition arg0) {   
                mMap.clear();               
                mMap.addMarker(new MarkerOptions().position(arg0.target));
        }
});

Option 1 选项1

I think the best option is move marker, when user move map... 我认为最好的选择是移动标记,当用户移动地图时......

googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                marker.setPosition(googleMap.getCameraPosition().target);//to center in map
            }
        });
//note marker is global

Option 2 选项2

or simple, in your layout, with relative layout put the fragment, and in above the icon of marker, you will calculate that marker will always in the middle of the map... 或简单,在您的布局中,相对布局放置片段,并在标记的图标上方,您将计算该标记将始终位于地图的中间...

i think this is complicate, by size of screens, but the phone will not move every time the marker... 我认为这很复杂,按屏幕大小,但手机每次都不会移动标记......

This would actually be possible by overlaying a custom view on top of your map. 实际上,这可以通过在地图上覆盖自定义视图来实现。 You would write a class that extends View and you could override the draw() and onTouchEvent() methods of your view, such that it would draw (what looks like) a marker, over top of the middle of the map. 您将编写一个扩展View的类,您可以覆盖视图的draw()和onTouchEvent()方法,以便在地图中间顶部绘制(看起来像)一个标记。 When the user sets the marker, you would use a projection to translate the screen location of the marker into a LatLong on the map. 当用户设置标记时,您将使用投影将标记的屏幕位置转换为地图上的LatLong。 You could then set a proper marker on the map (which would now be fixed to the geolocation). 然后,您可以在地图上设置正确的标记(现在将固定到地理位置)。

As you can tell from the above, this is not the simplest process. 从上面可以看出,这不是最简单的过程。 I would suggest going with doubleA's solution, for ease of implementation. 我建议使用doubleA的解决方案,以便于实施。

Marker marker;//have a instance variable marker
mMap.setOnCameraChangeListener(new OnCameraChangeListener();
public void onCameraChange(CameraPosition cameraPosition) {   
                MarkerOptions options = new MarkerOptions()
                                        .position(cameraPosition.target);

                 if(marker != null){marker.remove();}
                 marker = mMap.addMarker(options);
}

This won't be smooth because,onCameraChange call back is not called during intermediate frames.It is called only after the camera is changed completely. 这不会很顺利,因为在中间帧期间不会调用onCameraChange回调。只有在相机完全更换后才会调用它。

Get a marker reference when call addMarker. 调用addMarker时获取标记引用。 After when the camera moves, moves the marker to the target position returned by the camera. 相机移动后,将标记移动到相机返回的目标位置。

private Marker marker;

public void onMapReady(final GoogleMap googleMap) {
        LatLng sydney = new LatLng(-34, 151);
        MarkerOptions markerOptions = new MarkerOptions().position(sydney).title("Marker in Sydney");
        marker = googleMap.addMarker(markerOptions);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                final LatLng target = googleMap.getCameraPosition().target;
                marker.setPosition(target);
            }
        });
}

This is the solution 这是解决方案

    private GoogleMap mMap;
    private Marker markerCenter;

    @Override
    public void onMapReady(GoogleMap googleMap) {

       mMap = googleMap; 

       MarkerOptions markerOptions = new MarkerOptions();
       markerOptions.position(mMap.getCameraPosition().target);
       markerCenter = mMap.addMarker(markerOptions);

       mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
          public void onCameraMove() {
              markerCenter.setPosition(mMap.getCameraPosition().target);
          }
       });

    }

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

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