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

I can display the marker, as well as display it with the zoom and camera settings, when the first user views the map. But my requirement is to move the camera to the same marker position (when the user wants), if the user leaves this marker position (the marker goes beyond the screen) during his / her visit.

+6
source share
4 answers

Thanks for the answers, but I was looking for some native Map component to do the reset marker task, not the external button, to get back to the desired marker location. I got this working with the latest update in Map Api (to set setOnMyLocationButtonClickListener). Using the code below: -

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; } }); 
+9
source

With a link to a GoogleMap object and token, you can simply use

 GoogleMap mMap; Marker mMarker; [...] mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMarker.getPosition(), 14)); 

(where you pick up โ€œ14โ€ for your desired zoom level).

Just attach this line to the OnClick event of the button that the user clicks to โ€œreturnโ€ to the marker ... and you're done !;)

+16
source

you can use the [animateCamera] [1] function of the 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 
+5
source

You can also use the following:

 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 the 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); } 
+2
source

Source: https://habr.com/ru/post/952639/


All Articles