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.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.
Without clicking any button, how to directly get the current location and move the camera to it. Also, I found that there is a button on the right top of the map. When click on it, it will go to current location.
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;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With