简体   繁体   中英

Android - Google Maps - smoothly drag marker

I am trying to create a "Choose position" on map activity (like the one in the Google maps app, when choosing the starting point for directions).

Basically, I put a marker in the center of the map, then I re-position it on camera move. The problem is that the marker does not move smoothly (it's stuttering). I tried some solutions on stackoverflow, but none of them work.

My code is this:

    getGoogleMap().moveCamera(CameraUpdateFactory.newLatLngZoom(
            new LatLng(location.getLatitude(),
                    location.getLongitude()), 14));
    LatLng target = getGoogleMap().getCameraPosition().target;

    chooseMarker = getGoogleMap().addMarker(new MarkerOptions()
            .position(target));
    getGoogleMap().setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            // Get the center of the Map.
            LatLng centerOfMap = getGoogleMap().getCameraPosition().target;

            // Update your Marker's position to the center of the Map.
            chooseMarker.setPosition(centerOfMap);


        }
    });

If I understood your problem correctly, you want the user to select a location by using a marker that is following the camera position. Also, you want the marker to be in the center of the google map all the time. In that case, you can use a simple trick. Add a view on the top of your layout containing the google map, and make it invisible (assign the appropriate icon from the project's asset). Whenever the user wants to select a location, make it visible (it should be located at the center of your google map layout). Since we have access to the center of the map, once the user satisfies with the location, and submit the next action, we can move forward with the location of the center of the map. At the end of the process, make the 'marker' invisible.

Although there are other solutions, this might be very simple and efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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