[英]How do I set default location and Zoom level for google map api v2?
當我的地圖顯示時,它總是從固定的位置(非洲附近)開始。
然后,我使用以下代碼將地圖居中到我想要的位置。
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()), 14.0f));
我的問題是,我可以在地圖顯示之前設置默認位置和縮放級別嗎?
因為我不希望我的用戶在開始時看到動畫。
謝謝。
你可以使用它來直接縮放而不需要動畫:
map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx) , 14.0f) );
看一下這里的文檔:
https://developers.google.com/maps/documentation/android-api/map#configure_initial_state
根據您是通過XML添加地圖還是以編程方式添加地圖,這樣做的方式略有不同。 如果您使用的是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));
如果您想以編程方式在任何初始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" />
這將直接在特定Location
加載GoogleMap
,即initialLatLng。
我創建了一個SupportMapFragment
的句柄,並使用SupportMapFragment.getView().setVisibility()
將其visibility
設置為View.INVISIBLE
。 然后,在我的類實現的onLocationChanged
回調中,檢查是否為INVISIBLE
,如果為true則設置為VISIBLE
。 這可以消除您在加載時看到的跳躍,同時允許您動態初始化起始位置。 在我的情況下,我將相機圍繞用戶的位置居中,因此onLocationChanged
由於setMyLocationEnabled(true)
立即被調用。
您的要求可能不同,但無論哪種方式,您只需將可見性設置為INVISIBLE
,然后在加載適當的數據后找到一個設置VISIBLE
的好地方。
你可以使用static latlong使用directy CameraUpdate
LatLong latlong = new LatLong(lat, long);
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLong, 15);
mGoogleMap.moveCamera(cameraPosition);
mGoogleMap.animateCamera(cameraPosition);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.