gpt4 book ai didi

选项卡重叠问题中的Android Google map v2,选项卡中的多个 map

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:07 25 4
gpt4 key购买 nike

我是 android 的新手,现在正在使用 Google maps v2,我使用 tabhost 来显示 Google map v2,在这个 tabhost 中我需要在不同的选项卡上显示两个 Google maps v2。当我切换选项卡 b/w 两个包含 map 的选项卡时,它会重叠。我不知道要克服这个问题。

//两个选项卡的代码都在这里

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
Log.e("locamanager",""+locationManager);
Criteria criteria=new Criteria(); // object to retrieve provider
String provider = locationManager.getBestProvider(criteria, true);
Location location=locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 400, 1000, this);


}


@Override
public void onLocationChanged(Location location) {

latLng = new LatLng(location.getLatitude(), location.getLongitude());
Log.e("latlng",""+latLng);
cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);

new ReverseGeocodingTask(getBaseContext()).execute(latLng);

}

谁有解决办法请帮帮我

提前致谢

最佳答案

我遇到过同样的问题,但最后我用这种方式解决了。

首先,我创建了单独的 MyMapFragment 类,它扩展了 android.support.v4.app.Fragment

这里是onCreateView方法

if (view != null) {

ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}

try {
view = (ViewGroup) inflater.inflate(R.layout.map_frag, container,
false);
setUpMapIfNeeded();
showMap(maptype);
} catch (Exception e) {
}
return view;

然后我创建一个xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_fragmentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>

您可以将此布局文件添加到您的 map 布局位置

最后你必须在你的 Activity 类中使用这个添加 map View

MyMapFragment newFragment = new MyMapFragment (1);
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager().beginTransaction();
transaction.add(R.id.my_fragmentView, newFragment);
transaction.commit();

它在我这边工作得很好,我希望它对你有帮助。

MapFragment代码在这里

public class MapFragment extends Fragment {

private View view;
private GoogleMap mMapView;

public TouchableWrapper mTouchView;
LinearLayout map_fragg;

String userLat, userLon;

int maptype = 1;

Marker lastMarker = null;

public MapFragment() {
}

public MapFragment(int maptype) {
this.maptype = maptype;
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
getView();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

if (container == null) {
return null;
}

if (view != null) {

ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);

if (mTouchView != null) {
mTouchView.removeAllViews();
}

}

try {
mTouchView = new TouchableWrapper(getActivity());
view = (ViewGroup) inflater.inflate(R.layout.map_frag, null, false);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));

map_fragg = (LinearLayout) view.findViewById(R.id.map_fragg);
mTouchView.addView(view);
setUpMapIfNeeded();

} catch (Exception e) {
e.printStackTrace();
}

return mTouchView;

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

private void showMap(int maptype) {

switch (maptype) {
case 1:
setMapX();
break;
case 2:
setMapXX();
break;
case 3:
setUpMapXXX();
break;
default:
break;
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}

private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMapView == null) {
// Try to obtain the map from the SupportMapFragment.

mMapView = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMapView != null) {
setUpMap();
showMap(maptype);
}
}
}

private void setUpMap() {
mMapView.setMyLocationEnabled(true);
}

// Setting up map for MyProfile Page
private void setMapX() {
// Your Code for adding Markers
}

// Setting up map for MyProfile Page
private void setMapXX() {
// Your Code for adding markers
}


public class TouchableWrapper extends FrameLayout {

public TouchableWrapper(Context context) {
super(context);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
this.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return super.dispatchTouchEvent(ev);
}
}

@Override
public void onStop() {
super.onStop();

try {
SupportMapFragment f = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onDestroyView() {
super.onDestroyView();
}

@Override
public View getView() {
if (view != null) {
return view;
}
return super.getView();
}



private void setUpMapXXX() {
// Your Code for adding Markers
}

}

关于选项卡重叠问题中的Android Google map v2,选项卡中的多个 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16768174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com