java - Android MapView Map V2 在 Fragment 中

标签 java android google-maps android-fragments android-mapview

我一直在尝试让我的 map 正常工作,我的 FragmentTabHost atm 仅包含 3 个选项卡。在我的“ map ”选项卡中,它应该加载谷歌地图 fragment 并放大到用户位置,但它只加载 map 但不执行放大方法。另外,如果由于 map ID 重复而存在 Binrary XML 文件,当我返回“ map ”选项卡时,它会导致应用程序崩溃,我是否会修复该问题。

另一个问题是我可以采取什么方法来异步加载maptab。

public class Maps extends Fragment {

private GoogleMap googleMap;
MapView mapView;


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

    View view = inflater.inflate(R.layout.fragment_maps, container, false);

    mapView = (MapView) view.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    googleMap = mapView.getMap();
    MapsInitializer.initialize(this.getActivity());
    return view;
}

public void onActivityCreate(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    setUpMapIfNeeded();
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (googleMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (googleMap != null) {
            executeMap();
        }
    }
}

private void executeMap() {
    googleMap.setMyLocationEnabled(true);
    googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

    //Get location services from Google services and choose the best fit through criteria
    LocationManager locationMan = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Criteria bestFit = new Criteria();

    //Get name of best provider
    String provider = locationMan.getBestProvider(bestFit, true);
    //Get Location
    Location myLocation = locationMan.getLastKnownLocation(provider);

    //Get long and lat, create an object marker
    double latitude = myLocation.getLatitude();
    double longitude = myLocation.getLongitude();
    LatLng position = new LatLng(latitude, longitude);

    //Zoom into current location
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(position));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(18));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here"));
}

}

为什么不执行setupmaifneeded();

最佳答案

对于第一个问题:尝试使用以下方法将相机缩放到用户位置:

private void centerMapOnMyLocation() {

    map.setMyLocationEnabled(true);

    location = map.getMyLocation();

    if (location != null) {
        myLocation = new LatLng(location.getLatitude(),
                location.getLongitude());
    }
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,
            Constants.MAP_ZOOM));
} 

对于第二个问题:异步加载maptab是什么意思?您需要从数据库读取数据吗?我的建议是您可以在您的应用上添加一个 splashScreen 来稍等片刻,让数据加载。

关于java - Android MapView Map V2 在 Fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731187/

相关文章:

java - 每次调用 JLabel.setText() 都需要 EDT 吗?

java - 计算器不处理两个运算符方程

javascript - 如何使用 Google map 绘制多条折线?

javascript - 通过自动完成服务获取邮政编码

java - 超正方体 : Index out of bounds exceptions for OCR method

java - 如何在窗口上暂时阻止鼠标监听器?

Java HashTable size() 后跟 value() 在线程中安全吗?

android - 迁移到 androidx 后错误膨胀类 androidx.appcompat.widget.FitWindowsFrameLayout

Android Studio Android 模拟器无法启动。

javascript - 本地范围对象有时在带有 .then 的 JavaScript Promise 中工作