Issue
My function mimics "my location" -button of Google Maps. I call it using onClickListner
. I was able to add zoom-out and zoom-in animation to it, but I want it to work like the stock button.
My button:
ImageButton Mylocation = (ImageButton) findViewById(R.id.my_location);
Mylocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myPosition();
}
}
);
My function
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);
}
I want to add my custom button that points to "my location" with zoom- and scroll animation (like the stock button).
Solution
I did something similar in an app that i made. I used animateCamera and newLatLngZoom like so:
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(YourLat,YourLong),yourZoom);
you can read more about CameraUpdateFactory here and about animateCamera here
edit
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);
}
Answered By - Dor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.