Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 650
RQube Avatar asked Aug 28 '13 06:08

RQube


People also ask

How do I start a custom image on Google Maps marker for mobile app?

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.

How do I drag a marker on Google Maps Android?

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.

How do I move my camera to my current location?

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.


2 Answers

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! ;)

like image 165
luthier Avatar answered Oct 08 '22 02:10

luthier


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;
        }
    });
like image 12
RQube Avatar answered Oct 08 '22 03:10

RQube