答案 0 :(得分:2)
Ben P在他的评论中是正确的:“标记不会基于缩放更改大小”。但是,可以通过针对每个缩放级别删除(例如)onCameraIdle()
中具有自定义图标大小的标记并重新创建该标记,来实现此标记行为。使用:
(chair,chair)
和public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int DIAMETER_IN_METERS = 300;
private final LatLng MARKER_LOCATION = new LatLng(37.762555, -122.244221);
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
private Marker mMarker = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mMapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
if (mMarker != null) {
mMarker.remove();
}
mMarker = createResizedMarker(MARKER_LOCATION);
}
});
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(MARKER_LOCATION, 16));
}
private Marker createResizedMarker(LatLng marker_location) {
// get vector drawable
final Drawable iconDrawable = getResources().getDrawable(R.drawable.ic_letter_m);
// calc map resolution (meters per pixel) for current zoom level for details see e.g. https://msdn.microsoft.com/en-us/library/aa940990.aspx
final double meters_per_pixel = (Math.cos(mGoogleMap.getCameraPosition().target.latitude * Math.PI / 180) * 2 * Math.PI * 6378137) / (256 * Math.pow(2, mGoogleMap.getCameraPosition().zoom));
// calc marker icon diameter in pixels
final int diameter = (int)(DIAMETER_IN_METERS / meters_per_pixel);
// create marker icon bitmap for current scale
final Bitmap bitmap = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
iconDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
iconDrawable.draw(canvas);
Marker marker = mGoogleMap.addMarker(new MarkerOptions()
.position(MARKER_LOCATION)
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap)));
return marker;
}
}
-R.drawable.ic_letter_m
可绘制的矢量(用于完美缩放),例如:
xml
您得到类似的东西
但实际上,Ground Overlay可以完成所有工作。对于这样的任务,最好使用地面叠加层(至少要获得更好的性能和平滑缩放)。如果需要,例如万一需要绘制汽车运动,可以随时重新创建地面叠加层(具有不同的位图或位图大小,如上面的标记)。您还可以使用.setBearing()
旋转地面标记之类的标记。
答案 1 :(得分:2)
使用以下代码作为解决方案,我遇到了同样的问题。您可以将以下代码用于固定大小的标记。在我对此进行研究时,我们只有位图选项。
int height = 100;
int width = 100;
BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.mipmap.marker);
Bitmap b=bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
要添加市场,请使用以下代码:
gMap.addMarker(new MarkerOptions()
.position(POSITION)
.title("Title")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker))
);