Android Maps v2 custom marker has incorrect positi

2019-04-30 03:11发布

问题:

I know this has been discussed before, but I actually can't find a solution.

I am adding a custom icon for my marker:

markerUserLocation = mMap.addMarker(new
                        MarkerOptions().position(new
                                LatLng(point.latitude,
                                        point.longitude))
                                .anchor(0.5f, 1.0f)
                               .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_my_location)));

The problem is when zooming in/out the marker gets positioned in a bad place.

I have tried to play with the anchor values, but no success. Isn't the default anchor mid/bottom of the image ? What i need to do, to actually keep the bottom of the marker at the specified point coordinates, independent of zooming level ?

Here is a ic_marker_my_location icon sample (xhdpi size), as I can't upload the original icon.

LE: this is a really silly, but if i use a smaller marker image... it works correctly... So maybe there is a bug with marker resource image size ? Or if the image is bigger, I need to set some extra parameters ?

回答1:

I recently experienced a similar issue. It seems to me that there is a bug with the setIcon(Bitmap bitmap) function of the marker.

As soon as i change the image of the marker, the anchor is messed up, so I had to reset it again to the default position.

marker.setIcon(bitmap);
marker.setAnchor(0.5f,1f);

That worked for me using the play services (8.4.0)



回答2:

May be you are actually passing the float values for latitude and longitude. Please pass in the double values like this..

private double lat = 13.005039;
private double lng = 77.57734;


mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng));


回答3:

I managed to fix the issue by making a new marker png. Basically made a new png of the same size and recreated the image... and now it works. So I have no idea what really happened, maybe the map control didn't like some margins or something.



回答4:

I had the same issue, however the anchor solution posted by Mike T didn't work for me.

What worked for me was converting my original image into a vector (svg). This stopped my custom marker from moving on the map whenever I zoomed into or out from the map.



回答5:

used this code

// Creating a marker
                MarkerOptions markerOptions = new MarkerOptions();

                // Getting a place from the places list
                HashMap<String, String> hmPlace = list.get(i);

                // Getting latitude of the place
                double lat = Double.parseDouble(hmPlace.get("lat"));                

                // Getting longitude of the place
                double lng = Double.parseDouble(hmPlace.get("lng"));

                // Getting name
                String name = hmPlace.get("place_name");

                // Getting vicinity
                String vicinity = hmPlace.get("vicinity");

                LatLng latLng = new LatLng(lat, lng);

                // Setting the position for the marker
                markerOptions.position(latLng);

                // Setting the title for the marker. 
                //This will be displayed on taping the marker
                markerOptions.title(name);
                markerOptions.snippet(vicinity);

                // Placing a marker on the touched position
                mGoogleMap.addMarker(markerOptions);