gpt4 book ai didi

java - 如何添加模仿 "my location"-button 的 ImageButton?

转载 作者:行者123 更新时间:2023-12-02 03:38:57 26 4
gpt4 key购买 nike

我的功能模仿 Google map 的“我的位置”按钮。我使用 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 here 的更多信息以及关于 animateCamera here

编辑

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);


}

关于java - 如何添加模仿 "my location"-button 的 ImageButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56846770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com