首页 > 解决方案 > 如何添加一个模仿“我的位置”按钮的 ImageButton?

问题描述

我的功能模仿谷歌地图的“我的位置”按钮。我用onClickListner. 我能够为其添加缩小和放大动画,但我希望它像股票按钮一样工作。

我的按钮:

ImageButton Mylocation = (ImageButton) findViewById(R.id.my_location);
    Mylocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myPosition();
        }
    }
);

我的功能

public void myPosition() {
    if (gps.canGetLocation()) {
        latitude = gps.getLatitude();
        longitude = gps.getLongitude();
        // Toast.makeText(getApplicationContext(), "  "+latitude+" "+longitude,Toast.LENGTH_LONG).show();
        Longitude = Double.toString(latitude);
        Latitude = Double.toString(longitude);
    }
    // Add a marker in Sydney and move the camera
    LatLng me = new LatLng(latitude, longitude);
    //MyMarker= mMap.addMarker(new MarkerOptions().position(me).snippet("My Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(me));
    CameraUpdate center = CameraUpdateFactory.newLatLng(me);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(17);
    mMap.moveCamera(center);
    mMap.animateCamera(zoom);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling ActivityCompat#requestPermissions here to request the missing permissions, and then overriding public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) to handle the case where the user grants the permission. See the documentation for ActivityCompat#requestPermissions for more details.
        return;
    }

    mMap.setMyLocationEnabled(true);
}

我想使用缩放和滚动动画添加指向“我的位置”的自定义按钮(如股票按钮)。

标签: javaandroidgoogle-mapsandroid-layoutandroid-mapview

解决方案


我在自己制作的应用程序中做了类似的事情。我像这样使用 animateCamera 和 newLatLngZoom :

                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(YourLat,YourLong),yourZoom);

你可以在这里阅读更多关于 CameraUpdateFactory 和关于 animateCamera的信息

编辑

public void myPosition() {
        if (gps.canGetLocation()) {


            latitude = gps.getLatitude();
            longitude = gps.getLongitude();
            // Toast.makeText(getApplicationContext(), "  "+latitude+" "+longitude,Toast.LENGTH_LONG).show();
            Longitude = Double.toString(latitude);
            Latitude = Double.toString(longitude);
        }
        // Add a marker in Sydney and move the camera
        LatLng me = new LatLng(latitude, longitude);
        //MyMarker= mMap.addMarker(new MarkerOptions().position(me).snippet("My Location"));

         mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(me),17); //This is where it should be


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mMap.setMyLocationEnabled(true);


    }

推荐阅读