2013-01-04 76 views
79

當我的地圖顯示時,它始終始於固定位置(靠近非洲)。如何爲google map api v2設置默認位置和縮放級別?

然後,我使用以下代碼將地圖居中放置到我想要的位置。

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 14.0f)); 

我的問題是,可能我之前設置的地圖上顯示了默認位置,縮放級別?

因爲我不希望我的用戶在開始時看到動畫。

謝謝。

+0

這個qustion幫助....即使沒有啓用位置... animateCamera指向非洲的某個地方... wiered!對於這個問題,'moveCamera'是這個 – Devrath

+2

haha​​hhaha ....... + 1的最佳解決方案,你問我的心臟。 :D ......「它總是從一個固定的位置開始(在非洲附近)」........ rofll .....儘管如此,謝謝。 – akash89

回答

156

您可以使用此情況下直接動畫縮放:

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f)); 
+0

謝謝,這正是我需要的:) – dong221

+0

我有最小的經度/緯度和最大的緯度/經度那麼如何設置? –

+0

你可以添加新的問題,所以我可以發佈你的完整答案,但總之你將不得不使用LatLngBounds.Builder來建立邊界並使用它。 –

80

看看文檔在這裏:

https://developers.google.com/maps/documentation/android-api/map#configure_initial_state

做略有不同,這取決於是否方式您正在通過XML或編程方式添加地圖。

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    map:cameraBearing="112.5" 
    map:cameraTargetLat="-33.796923" 
    map:cameraTargetLng="150.922433" 
    map:cameraTilt="30" 
    map:cameraZoom="13"/> 

如果以編程方式做它,你可以執行以下操作:

CameraPosition cameraPosition = new CameraPosition.Builder() 
    .target(new LatLng(-33, 150)) 
    .zoom(13) 
    .build(); 
MapFragment.newInstance(new GoogleMapOptions() 
    .camera(camera)); 
+0

Great Answer !!! – Ejmedina

+1

很好的答案。其實這個更適合這個問題。 – MightySeal

+2

in xml我得到lint錯誤'意外的命名空間前綴'映射「找到標記片段' –

3

我創建了一個句柄SupportMapFragment並設置其visibility如果使用XML,你可以按如下做到View.INVISIBLE使用SupportMapFragment.getView().setVisibility()。然後,在我的類實現的onLocationChanged回調中,我檢查是否INVISIBLE並且如果爲true,則設置爲VISIBLE。這可以消除您在加載時看到的跳躍,同時允許您動態初始化開始位置。在我的情況下,我將相機對準用戶位置,所以onLocationChanged由於setMyLocationEnabled(true)而立即被調用。

你的要求可能會有所不同,但無論哪種方式,你只需將可見性設置爲INVISIBLE,然後找到一個好地方,設置VISIBLE您適當的數據加載後。

1

您可以使用靜態經緯度

LatLong latlong = new LatLong(lat, long); 
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLong, 15); 
       mGoogleMap.moveCamera(cameraPosition); 
       mGoogleMap.animateCamera(cameraPosition); 
3

使用directy的CameraUpdate以防萬一你想在任何初始Location程序加載Google Map那麼你可以使用這段代碼,

FragmentManager fm = getFragmentManager(); // getChildFragmentManager inside fragments. 
CameraPosition cp = new CameraPosition.Builder() 
        .target(initialLatLng) // your initial co-ordinates here. like, LatLng initialLatLng 
        .zoom(zoom_level) 
        .build(); 
SupportMapFragment mapFragment = SupportMapFragment.newInstance(new GoogleMapOptions().camera(cp)); 
fm.beginTransaction().replace(R.id.rl_map, mapFragment).commit(); 

添加此一段代碼爲layout

<RelativeLayout 
     android:id="@+id/rl_map" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

這將直接加載GoogleMap,特別是Location,即initialLatLng。

相關問題