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

邮差的信 提交于 2019-11-27 03:18:12

问题


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

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)

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?


回答1:


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. Then when your user selects a location just retrieve the gps coordinates from the middle of the screen.

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)); 



回答2:


How about?

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



回答3:


Option 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

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...




回答4:


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. 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. 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.




回答5:


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);
          }
       });

    }



回答6:


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.




回答7:


Get a marker reference when call 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);
            }
        });
}


来源:https://stackoverflow.com/questions/14152594/google-map-api-v2-how-do-i-keep-a-marker-in-the-center-of-the-screen-while-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!