繁体   English   中英

如何添加模仿“我的位置”按钮的 ImageButton?

[英]How to add an ImageButton that mimics "my location" -button?

我的功能模仿“我的位置” - 谷歌地图的按钮。 我使用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);
}

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

我在我制作的应用程序中做了类似的事情。 我使用 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);


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM