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

Pangu

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?

Antrromet

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?

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Android google maps v2 execute asynctask when user stops moving the camera

分類Dev

Google Maps Android API v2

分類Dev

How to manage Markers well using google maps api v2 on android

分類Dev

Google Maps Android API v2 Authorization failure

分類Dev

Can't connect to Google Maps Api Android V2

分類Dev

Overlay an image on google maps android api v2

分類Dev

Google Maps Activity V2 Android Studio

分類Dev

How to set api key for Google maps v2

分類Dev

Is the location displayed on screen - google maps on android

分類Dev

How to change Language on Android google map V2 api

分類Dev

Android 2.3およびgoogle maps API v2

分類Dev

Circle on Google Maps for Android v2 is flickering when size changes by seekbar

分類Dev

Android Google Maps API v2:方位位置を取得する

分類Dev

Adding Multiple Markers in real time google maps v2 android

分類Dev

Android Google Maps go to current location without animation

分類Dev

Android Google Maps LocationManager returns a location I was at two hours ago

分類Dev

Recognise click over current location marker in Android Google Maps

分類Dev

Google android maps api v2マーカータイトルを常に表示

分類Dev

Animated Transparent Circle on Google Maps v2 is NOT animating correctly

分類Dev

Google maps v2 not displayed on new activity

分類Dev

Google Maps V2 - API key not found Error Message

分類Dev

How to show enable location dialog like Google maps?

分類Dev

How to show search results of Google Maps embed at current location?

分類Dev

android maps api v2 adding multiple circles

分類Dev

access maps v2 with calabash-android

分類Dev

access maps v2 with calabash-android

分類Dev

How to download and use offline maps in Map API V2?

分類Dev

How to animate camera.lookAt using gsap?

分類Dev

Google Maps Android API v2のMapFragmentにカスタムコントロールを追加する方法

Related 関連記事

  1. 1

    Android google maps v2 execute asynctask when user stops moving the camera

  2. 2

    Google Maps Android API v2

  3. 3

    How to manage Markers well using google maps api v2 on android

  4. 4

    Google Maps Android API v2 Authorization failure

  5. 5

    Can't connect to Google Maps Api Android V2

  6. 6

    Overlay an image on google maps android api v2

  7. 7

    Google Maps Activity V2 Android Studio

  8. 8

    How to set api key for Google maps v2

  9. 9

    Is the location displayed on screen - google maps on android

  10. 10

    How to change Language on Android google map V2 api

  11. 11

    Android 2.3およびgoogle maps API v2

  12. 12

    Circle on Google Maps for Android v2 is flickering when size changes by seekbar

  13. 13

    Android Google Maps API v2:方位位置を取得する

  14. 14

    Adding Multiple Markers in real time google maps v2 android

  15. 15

    Android Google Maps go to current location without animation

  16. 16

    Android Google Maps LocationManager returns a location I was at two hours ago

  17. 17

    Recognise click over current location marker in Android Google Maps

  18. 18

    Google android maps api v2マーカータイトルを常に表示

  19. 19

    Animated Transparent Circle on Google Maps v2 is NOT animating correctly

  20. 20

    Google maps v2 not displayed on new activity

  21. 21

    Google Maps V2 - API key not found Error Message

  22. 22

    How to show enable location dialog like Google maps?

  23. 23

    How to show search results of Google Maps embed at current location?

  24. 24

    android maps api v2 adding multiple circles

  25. 25

    access maps v2 with calabash-android

  26. 26

    access maps v2 with calabash-android

  27. 27

    How to download and use offline maps in Map API V2?

  28. 28

    How to animate camera.lookAt using gsap?

  29. 29

    Google Maps Android API v2のMapFragmentにカスタムコントロールを追加する方法

ホットタグ

アーカイブ