在谷歌Map(android)中缩放Map时位置错误

igetnqfo  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(360)

我在android studio中实现了一个Map,但是当我缩放标记时,它会转到错误的位置。

这是maps类,我创建了一个画布,然后转换成位图:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private ActivityMapsBinding binding;
    private Bitmap bm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bm = createImageRounded(getApplicationContext(), 80, 80, "S");
        binding = ActivityMapsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        ImageView imageView = (ImageView)findViewById(R.id.img);
        LatLng sydney = new LatLng(-33.8688, 151.2093);
        MarkerOptions mrk = new MarkerOptions().title("Sydney");
        mrk.position(sydney);
        mrk.icon(BitmapDescriptorFactory.fromBitmap(bm));
        mrk.anchor(0.5f, 1);

        mMap.addMarker(mrk);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    }

    public static Bitmap createImageRounded(Context context, int width, int height, String name)
    {
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        paint.setColor(Color.rgb(62, 176, 220));
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

        canvas.drawCircle(30, 30, 10, paint);

        return output;
    }

}

非常感谢你的帮助

798qvoo8

798qvoo81#

这是因为您正在绘制比标记边界更小的图像-您正在创建 Bitmap 尺寸为80x80,但在其上绘制较小的圆(10半径,不居中,请尝试 canvas.drawCircle(width/2, height/2, Math.min(width, height)/2, paint); )
此外,您设置了错误的锚点(未居中,请尝试设置 mrk.anchor(0.5f, 0.5f); )

相关问题