看起来我已经创建了一个模仿Google Map API的位置按钮的函数,然后使用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);
}
我想要的是添加自己的自定义按钮,该按钮指向我的位置,并且还具有缩放和滚动动画(如在股票按钮中一样)
答案 0 :(得分:0)
我在自己制作的应用中做了类似的事情。 我像这样使用animateCamera和newLatLngZoom:
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(YourLat,YourLong),yourZoom);
您可以了解有关CameraUpdateFactory here的更多信息 关于animateCamera here
修改
public void myPosition(){ 如果(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);
}