Rehan Yousaf
Rehan Yousaf

Reputation: 245

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.

Upvotes: 8

Views: 10627

Answers (3)

Rahim Rahimov
Rahim Rahimov

Reputation: 1417

Yep, actually you can use setOnCameraChangeListener. So you don't need to clear your marker every time, just set new position for it. Try this code:

map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition cameraPosition) 
    {
        if(marker==null)
        {
            marker = map.addMarker(new MarkerOptions().position(cameraPosition.target).title("Marker")
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.marker)));
            marker.showInfoWindow();
        }
        else
        {
            marker.setPosition(cameraPosition.target);
        }
    }
});

This should work. So, the only problem is that your marker will move to centre of the map only at the end of map dragging. To avoid this problem you can just use something like ImageView and paste it at the centre of your map Fragment. Thereafter you can use the code above with adding just:

marker.setVisible(false);

Upvotes: 1

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10298

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.

Upvotes: 6

Zahid Rasheed
Zahid Rasheed

Reputation: 1554

Assuming you are using Google Play Services for getting user's location call below from

@Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "Connected to GoogleApiClient");

        // If the initial location was never previously requested, we use
        // FusedLocationApi.getLastLocation() to get it. If it was previously requested, we store
        // its value in the Bundle and check for it in onCreate(). We
        // do not request it again unless the user specifically requests location updates by
        // pressing
        // the Start Updates button.
        //
        // Because we cache the value of the initial location in the Bundle, it means that if the
        // user launches the activity,
        // moves to a new location, and then changes the device orientation, the original location
        // is displayed as the activity is re-created.
        if (mCurrentLocation == null) {
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            drawOnMap(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
        }
    }

And

/**
     * Callback that fires when the location changes.
     */
    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        if (location != null) {
            drawOnMap(new LatLng(location.getLatitude(), location.getLongitude()));
        }
    }

This will be the function to update marker on the map.

   public void drawOnMap(final LatLng location) {
        if (isFirstLocation) {
            markerCurrent = mMap.addMarker(new MarkerOptions()
                    .position(location)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.dot)));
            isFirstLocation = false;
        } else {
            markerCurrent.setPosition(location);
        }
        CameraPosition cp = getCameraPosition(location);
        int animationDuration = 600;
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp), animationDuration, null);
    }

Upvotes: -1

Related Questions