【问题标题】:Android MapView Map V2 within FragmentFragment内的Android MapView Map V2
【发布时间】:2015-02-27 08:29:33
【问题描述】:

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

另一个问题是,我可以采用什么方法来异步加载 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();

【问题讨论】:

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


【解决方案1】:

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

对于second issueload the maptab asynchronously 是什么意思?您需要read data 形成database 吗?我的建议是你可以在你的应用程序上添加一个splashScreen 等待片刻让数据加载。

【讨论】:

  • 如果你看我的代码,为什么它不执行setupmyifneeded()?因为我希望它加载并放大到用户位置
  • 我在onActivityCreate中看到了你的setupmyifneeded(),你最后漏掉了一个d@override方法叫onActivityCreated
【解决方案2】:

发生应用程序崩溃是因为您没有使用接口在片段之间进行通信。现在它们紧密耦合,因此存在内存泄漏。请参考this指南了解更多关于android中片段间通信的信息。代码实现参考this指南。

希望有帮助!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 2013-06-17
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多