我公司采用新的地图API。其做工精细.. 但是,当设备方向改变..地图变得反应迟钝。我的意思是地图不应对用户的响应。
这,问题不会当我嵌入在布局XML映射片段发生。但只有当我试图添加mapfragment在活动的onCreate布局容器发生。
谁能告诉我这个问题。
下面的方式,我将地图
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=HomeActivity。>
<的FrameLayout
机器人:ID =@ + ID / activity_home_fragment_container
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =match_parent
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真正的>
< /的FrameLayout>
< / RelativeLayout的>
而在活动的OnCreate
mMapFragment =新SupportMapFragment();
FragmentManager经理= getSupportFragmentManager();
FragmentTransaction交易= manager.beginTransaction();
transaction.add(R.id.activity_home_fragment_container,mMapFragment,
MAP_FRAGMENT);
器transaction.commit();
解决方案
在布局中使用这样的:
<的LinearLayout
机器人:ID =@ + ID / map_container2
机器人:layout_width =match_parent
机器人:layout_height =200dp
机器人:layout_weight =35.22
机器人:方向=横向>
<片段
机器人:ID =@ + ID / MAP1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1
类=com.google.android.gms.maps.SupportMapFragment
图:cameraTargetLat =40.72
图:cameraTargetLng = - 74.00
图:cameraZoom =8/>
< / LinearLayout中>
这code:
的onCreate {
setUpMapIfNeeded();
}
私人无效setUpMapIfNeeded(){
// TODO自动生成方法存根
//做一个空检查确认,我们还没有实例化的地图。
如果(MMAP == NULL){
//尝试获取来自SupportMapFragment地图。
MMAP =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map1))
.getMap();
//检查如果我们成功取得地图。
如果(MMAP!= NULL){
setUpMap();
}
}
}
私人无效setUpMap(){
// TODO自动生成方法存根
//隐藏缩放控件按钮面板将覆盖它。
mUiSettings = mMap.getUiSettings();
//启用/禁用变焦手势(即,双击,捏和放大器;拉伸)。
mMap.getUiSettings()setZoomGesturesEnabled(假)。
//启用/禁用滚动手势(即平移地图)。
mMap.getUiSettings()setScrollGesturesEnabled(假)。
//启用/禁用指南针(图标左上方,指示的方向
// 地图)。
mMap.getUiSettings()setCompassEnabled(假)。
//添加大量的标记到地图中。
addMarkersToMap();
//潘看到视野中的所有标记。
//无法放大到边界,直到地图的大小。
最后查看图形页面= getSupportFragmentManager()findFragmentById(R.id.map1).getView()。
如果(mapView.getViewTreeObserver()。的isAlive()){
mapView.getViewTreeObserver()。addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@燮pressLint(NewApi)//我们检查哪些构建我们使用的版本。
@覆盖
公共无效onGlobalLayout(){
的LatLngBounds边界=新LatLngBounds.Builder()
.INCLUDE(WOODS)
。建立();
如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
。mapView.getViewTreeObserver()removeGlobalOnLayoutListener(本);
} 其他 {
。mapView.getViewTreeObserver()removeGlobalOnLayoutListener(本);
}
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(范围,50));
}
});
}
}
私人无效addMarkersToMap(){
// TODO自动生成方法存根
//使用彩色图标。
mWoods = mMap.addMarker(新MarkerOptions()
.position(WOODS)
.title伪(伍兹)
是.snippet(:R夸塔,1016,维拉奥林匹亚 - (11)3849-6868)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}
I am using new maps api. its working fine.. But when the device orientation changes.. the map become unresponsive. i mean map doesn't react to the user response.
This, problem doesn't occurs when i embed the map fragment in the layout xml. but occurs only when i am trying to add the mapfragment in the layout container in the Activity's onCreate.
Can anyone tell me the problem.
Here the way i am adding the map
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity" >
<FrameLayout
android:id="@+id/activity_home_fragment_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</FrameLayout>
</RelativeLayout>
And in Activity's oncreate
mMapFragment = new SupportMapFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.activity_home_fragment_container, mMapFragment,
"MAP_FRAGMENT");
transaction.commit();
解决方案
Use this in your layout:
<LinearLayout
android:id="@+id/map_container2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="35.22"
android:orientation="horizontal" >
<fragment
android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="40.72"
map:cameraTargetLng="-74.00"
map:cameraZoom="8" />
</LinearLayout>
And this code:
onCreate{
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// TODO Auto-generated method stub
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// TODO Auto-generated method stub
// Hide the zoom controls as the button panel will cover it.
mUiSettings = mMap.getUiSettings();
// Enables/disables zoom gestures (i.e., double tap, pinch & stretch).
mMap.getUiSettings().setZoomGesturesEnabled(false);
// Enables/disables scroll gestures (i.e. panning the map).
mMap.getUiSettings().setScrollGesturesEnabled(false);
// Enables/disables the compass (icon in the top left that indicates the orientation of the
// map).
mMap.getUiSettings().setCompassEnabled(false);
// Add lots of markers to the map.
addMarkersToMap();
// Pan to see all markers in view.
// Cannot zoom to bounds until the map has a size.
final View mapView = getSupportFragmentManager().findFragmentById(R.id.map1).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi") // We check which build version we are using.
@Override
public void onGlobalLayout() {
LatLngBounds bounds = new LatLngBounds.Builder()
.include(WOODS)
.build();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
}
});
}
}
private void addMarkersToMap() {
// TODO Auto-generated method stub
// Uses a colored icon.
mWoods = mMap.addMarker(new MarkerOptions()
.position(WOODS)
.title("Woods")
.snippet("R. Quatá, 1016, Vila Olimpia - (11) 3849-6868")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
}