簡體   English   中英

如何在Google Maps v2 for Android中將相機設置為指定位置?

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

我無法將Google地圖相機移回初始位置。 基本上,我在Actionbar上有一個主頁按鈕。 如果用戶在地圖上滾動並點擊“主頁”按鈕,我希望相機移回原始位置。

我使用googleMap.getCameraPosition().target獲取原始坐標,但相機不移動。

@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>

我錯過了什么?

每當用戶選擇該選項時,您正在使用

LatLng initialLoc= mMap.getCameraPosition().target;

得到所謂的初始位置,這是錯誤的! mMap.getCameraPosition()。target返回攝像頭指向的位置。 您應該根據您的其他代碼將lat long存儲在活動的onCreate()或其他位置,然后在onOptionItemSelected()使用相同的內容。

順便說一下,你可以在一個語句中將zoom和lat long結合起來,如下所示。

    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

我真的不知道它是多么准確或什么時候打電話給它。 但是你可以使用相同的代碼

LatLng initialLoc= mMap.getCameraPosition().target;

而是在onCreate()onResume()調用一次,然后將其存儲在那里。 然后下次在您的optionsItemSelected()使用這些值。 雖然,為什么不簡單地將您在xml中定義的值存儲在java代碼中然后使用它?

     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)); 
LatLng coordinate = new LatLng(latitude, longitude);
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(coordinate);
                    markerOptions.title(placeName); //Here Total Address is address which you want to show on marker
                    mGoogleMap.clear();
                    markerOptions.icon(
                            BitmapDescriptorFactory
                                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

                    markerOptions.getPosition();
                    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
                    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
                    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM