Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate the camera to a specified location in Google Maps v2 for Android?

I'm having trouble moving the Google Maps camera back to the initial position. Basically, I have a home button on the Actionbar. If a user scrolls around the map, and taps the Home button, I would like the camera to move back to its original location.

I get the original coordinates using googleMap.getCameraPosition().target, but the camera doesn't move.

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    LatLng initialLoc= mMap.getCameraPosition().target;

    CameraUpdate update = CameraUpdateFactory.newLatLng(initialLoc);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

    mMap.moveCamera(update);
    mMap.animateCamera(zoom);

    return true;
}

map.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context="org.test"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        map:cameraTargetLat="xxx.xxx"
        map:cameraTargetLng="xxx.xxx"
        map:cameraZoom="15"
        map:mapType="normal" />

</RelativeLayout>

What is it that I am missing?

like image 263
Pangu Avatar asked Aug 22 '15 23:08

Pangu


People also ask

How do you animate a camera on Google Maps?

To apply a CameraUpdate to the map, you can either move the camera instantly or animate the camera smoothly. To move the camera instantly with the given CameraUpdate , you can call GoogleMap. moveCamera(CameraUpdate) . The CameraUpdate describing where to move the camera.


2 Answers

Whenever the user selects the option, you are using

LatLng initialLoc= mMap.getCameraPosition().target;

to get the supposedly initial location, which is wrong! mMap.getCameraPosition().target returns the location that the camera is pointing at. You should store the lat long in the onCreate() of the activity or some other place as per your other code, and then use the same in onOptionItemSelected().

Btw you can combine the zoom and lat long in a single statement as follows.

    LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
    CameraUpdate location = CameraUpdateFactory.newLatLngZoom(
            coordinate, 15);
    mMap.animateCamera(location);

UPDATE

I dont really know as to how accurate that'd be or when to call it. But you could use the same code

LatLng initialLoc= mMap.getCameraPosition().target;

Instead call this once in your onCreate(), or onResume() and then store it there. Then next time in your optionsItemSelected() use those values. Although, why don't you simply store those values that you defined in your xml in java code and then use it?

like image 102
Antrromet Avatar answered Sep 28 '22 08:09

Antrromet


     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());  
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
     mMap.clear();


    markerOptions.icon(
    BitmapDescriptorFactory
   .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)); 

     markerOptions.getPosition(); 
     mCurrLocationMarker = mMap.addMarker(markerOptions); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 
like image 22
SUBODH KUMAR Avatar answered Sep 28 '22 08:09

SUBODH KUMAR