简体   繁体   English

片段内的Android MapView Map V2

[英]Android MapView Map V2 within Fragment

I have been trying to get my Maps working, I have FragmentTabHost atm only contains 3 tabs. 我一直在尝试使我的地图正常工作,但我的FragmentTabHost atm仅包含3个标签。 In my Maps tab, it should load the google map fragment and zoom into the users location however it just loads the map but does not do the zoom in method. 在我的地图标签中,它应该加载google地图片段并放大到用户位置,但是它只是加载地图而没有执行放大方法。 Also is there I fix if there is a Binrary XML file due to map ID duplicates it crashes the app when I return back to the Maps tab. 还可以解决由于映射ID重复而导致存在二进制XML文件的问题,当我返回到“地图”选项卡时,它会使应用程序崩溃。

Another question is that what method can I take to be able to load the maptab asynchronously. 另一个问题是我可以采用哪种方法来异步加载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"));
}

}

Why does it not execute setupmaifneeded(); 为什么不执行setupmaifneeded(); ?

For the first issue : try to use following method to zoom the camera to user's location : 对于第first issue :请尝试使用以下方法zoom the camera to user's location

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));
} 

For the second issue : what do you mean by load the maptab asynchronously ? 对于second issueload the maptab asynchronously是什么意思? Do you need to read data form database ? 您需要从database read data吗? My suggest is you can add a splashScreen on your app to wait a moment let the data to be loaded. 我的建议是,您可以在应用程序上添加splashScreen ,稍等片刻即可加载数据。

The app crash is happening because of the fact that you are not using interfaces in order to communicate in between the fragment. 由于您没有使用接口来在片段之间进行通信,因此发生了应用程序崩溃。 Right now they are tightly coupled so there is a memory leakage. 目前,它们紧密耦合,因此存在内存泄漏。 Please refer to this guide to know more about the inter fragment communication in android. 请参阅指南以了解有关android中片段间通信的更多信息。 For code implementation refer to this guide. 有关代码实现,请参阅指南。

Hope that helps!! 希望有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM