java - 如何添加一个模仿“我的位置”按钮的 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);
}
我想使用缩放和滚动动画添加指向“我的位置”的自定义按钮(如股票按钮)。
解决方案
我在自己制作的应用程序中做了类似的事情。我像这样使用 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);
}
推荐阅读
- python - 当我双击 .py 文件时 GUI 会打开,但是当我在 pycharm 中运行它时不会
- php - Laravel - 如何修复 Laravel 8 和 Office365 中的“无法在 SMTP 服务器上进行身份验证”?
- python - skimage KeyError:'axis_major_length'
- ios - 如何防止我的部分标题在组合布局中重叠单元格
- r - 对某个变量的数字范围进行分组
- nginx - NGINX - 使用 sub_filter 更改代理传递位置块中的路径
- json - Groovy:比较两个惰性映射/json
- javascript - 参数化 on() 函数 jquery
- python - 使用两个线程同时操作 ptz 轴
- reactjs - Stripe React Native:商家显示名称不能为空字符串