Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the map marker in the center of the screen at all times? Android

I am trying to find a way to center my map marker in my map fragment activity. So far, I have tried adding a marker to the center of the screen every time the map is moved i.e., the camera position is updated. But the problem with this solution is that every time a new marker gets added into the map at the center and the old one stays there so in just a couple of drags i would have like 10 markers on my screen. I tried using the clear method before adding the next marker but now the marker is just flashing too much. Here's my code:

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
                LatLng location=mMap.getCameraPosition().target;
                MarkerOptions marker=new MarkerOptions().position(location).title("");
                mMap.clear();
                mMap.addMarker(marker);
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 16));
            }
        });

The other solution i have found is to use a centered ImageView but the problem with that is that i want to change the icon of my marker. But when i add an image image to the image view, the center of the camera is in the green little circle in the center of this image and not the pointy end of it like i want to. Please help.

like image 701
Rehan Yousaf Avatar asked Jul 12 '16 19:07

Rehan Yousaf


People also ask

How do I drag a marker on Google Maps Android?

Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.

What is the marker on Google Maps called?

The Google Maps pin is the inverted-drop-shaped icon that marks locations in Google Maps. The pin is protected under a U.S. design patent as "teardrop-shaped marker icon including a shadow".

Can you leave markers on Google Maps?

If you want to show people where you are, add a marker in Google Maps. Once you've placed the marker, you can create a custom link to share with friends or embed your map on a Web page. All you need is a free Google account.


1 Answers

Don't use a real marker. Put the MapFragment or MapView in a layout with a fake marker image centered over it. When the location is chosen, place a real marker and hide the fake one.

If the "pointy end" of the fake marker image is not in its exact center, simply pad the image with transparent space until it is.

like image 151
Kevin Krumwiede Avatar answered Sep 28 '22 20:09

Kevin Krumwiede