GoogleMap change the camera position on zoom - android

In my app I use GoogleMap (play-services-maps:10.2.1). I've fixed the position of the map on a specific location and I don't want my user to be able move the map. I only want him to be able to zoom on it.
Here's what I tried :
// Set position
LatLng requestedPosition = new LatLng(lat, lon);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
// Disable all Ui interaction except for zoom
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(true);
It looks like it work at first sight but in fact while zooming and dezooming the camera position change a little on every zoom movement.
I have no idea what to do.
Thanks for your help

If I understand correctly, you would like to preserve a centre position after the zoom gestures. Zooming with the gesture doesn't maintain the same centre, you should correct position of camera once the gesture zoom is over. You can listen the idle event after zooming and animate the camera to the initial centre position.
Code snippet
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnCameraIdleListener {
private GoogleMap map;
private LatLng requestedPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
// Add a marker in Sydney and move the camera
requestedPosition = new LatLng(41.385692,2.163953);
float zoom = 16.0f;
map.addMarker(new MarkerOptions().position(requestedPosition).title("Marker in Barcelona"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
//map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
// Disable all Ui interaction except for zoom
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(true);
map.setOnCameraIdleListener(this);
}
#Override
public void onCameraIdle() {
float zoom = map.getCameraPosition().zoom;
map.animateCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
}
}
I placed this sample at Github https://github.com/xomena-so/so43733628
Hope this helps!

Related

show particular country as default in google map android

Is there any way by which we can show the particular country as default country in google map?
I have tried with the centre of some of the countries but it seems tedious.
I have only country name say "India" and I need to do is, show "India" in focus when the app gets open.
In your onMapReady() function set
LatLngBounds boundsIndia = new LatLngBounds(new LatLng(23.63936, 68.14712), new LatLng(28.20453, 97.34466));
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundsIndia, padding);
mMap.animateCamera(cameraUpdate);
When you open your map you will see only India country on map.
You can do it for any other countries.
This shows Sydney and puts a marker on it https://developers.google.com/maps/documentation/android-api/map-with-marker
public class MapsMarkerActivity extends AppCompatActivity
implements OnMapReadyCallback {
// Include the OnCreate() method here too, as described above.
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

Animation start from Maps

I have make a Map with 30 Overlays that will zoom in at start.
But the Animation (zoom in) is showing before the tiles from the Map are showing. After the Animation, Map is loading the tiles. It is happening everytime. Also after a destroy and reload.
Is there any way to say that the Animation starts after the tiles are loaded ?
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(52.33101, 7.91667 )) // Sets the center of the map to Mountain View
.zoom(9)
.bearing(0)
.tilt(60)
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 10000, null);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(52.2056, 7.70862))
Try following:
mMap.setOnMapLoadedCallback(this);
The method can look something like:
#Override
public void onMapLoaded() {
if (mMap != null) {
// animate the camera to desired position
}
}
For more info, following is the link:
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Move and zoom camera android maps v2 on Nexus7

I've faced strange behaviour of Google Map v2 on my Nexus7 when trying to place camera in specified position.
Code:
public class PlacesFragment extends MapFragment {
GoogleMap mapView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mapView = getMap();
mapView.moveCamera(
CameraUpdateFactory.newLatLng(new LatLng(50.4293817, 30.5316606)));
mapView.moveCamera(CameraUpdateFactory.zoomTo(11));
}
This piece of code moves camera to specified position on Nexus4, but on Nexus7 2013 it moves camera to (19.1599396,30.5316606) position, which has right longitude, but not latitude.
I've found a workaround, but I'm still interested why this happens.
For those who have this problem this is the solution:
mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.4293817, 30.5316606), 11));

how to move camra to a specific marker in google maps v2 in android

I am able to display a marker as well as showing it with zoom and camera setting when first time user is viewing the map. But my requirement is to move the camera to same marker position(when user want) if user goes away from that marker position(marker gets off-screen) during his/her visit.
Having a reference to the GoogleMap object and to the Marker, you can simply use
GoogleMap mMap;
Marker mMarker;
[...]
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMarker.getPosition(), 14));
(where you would substitute the "14" for your desired level of zoom).
Just attach that line to the OnClick event of the button that the user will click to "get back" to the marker... and you're done! ;)
Thanks for replies,
but i was looking for some native Map component to perform the map marker reset task rather than an external button to navigate back to desired marker location. I got this working with the latest update in Map Api(to have setOnMyLocationButtonClickListener) Using below code:-
mMap.setMyLocationEnabled(true);
LatLng markerLoc=new LatLng(companyDetail.getLatitude(), companyDetail.getLongitude());
final CameraPosition cameraPosition = new CameraPosition.Builder()
.target(markerLoc) // Sets the center of the map to Mountain View
.zoom(13) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); //
mMap.addMarker(new MarkerOptions().position(new LatLng(companyDetail.getLatitude(), companyDetail.getLongitude())).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick() {
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return true;
}
});
you can use [animateCamera][1] function of GoogleMap object
GoogleMap googleMap = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map).getMap();
googleMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
[1]: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#animateCamera%28com.google.android.gms.maps.CameraUpdate%29
You can also use like this :
LatLng cur_Latlng=new LatLng(21.0000,78.0000); // giving your marker to zoom to your location area.
gm.moveCamera(CameraUpdateFactory.newLatLng(cur_Latlng));
gm.animateCamera(CameraUpdateFactory.zoomTo(4));
// another method is to use current location
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 4);
gm.animateCamera(cameraUpdate);
Marker myMarkerthirtyfour = gm.addMarker(new MarkerOptions()
.position(latLng)
.title("You are here")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
locationManager.removeUpdates(this);
}

How do I change the result of mylocationoverlay?

I am using mylocationoverlay to give my current location, however, its not exactly accurate. Sometimes it is up to 20 meters wrong. I want to be able to move the location marker to my exact position. When using the following code, when I press on the marker, the map moves and the marker stays in the same place. How do I keep the map static and allow a user to move the marker?
public class selectmap extends MapActivity {
private MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectmap);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.enableMyLocation();
mapView.getController().setZoom(17);
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
}
}
You have to write your own implementation to handle this scenario. Here is an example application where you can move the markers
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/NooYawkTouch/

Categories

Resources