如何在Fragment Here Map API中使用MapFragment

时间:2018-09-18 12:33:15

标签: android api android-fragments here-api

我的应用程序有4个用于 ViewPager 的标签,在其中一个标签中,我想显示 HERE MAPS 。我能够使地图在Activity中完美运行,但是在Fragment Class中,当我尝试将Fragment视图投射到MapFragment时会引发错误。

这是我的XML代码示例

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:background="@android:color/transparent">
<!-- Map Fragment embedded with the map object -->
  <fragment
    android:id="@+id/mapfragment"
    class="com.here.android.mpa.mapping.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  />
</LinearLayout>

在我的“片段代码”样本下面

public class CurrentLocationFragment extends Fragment {
// map embedded in the map fragment
private MapFragment mapFragment = null;
private Map map = null;
private static MapRoute mapRoute1;
private String locationAddress = "";
private Double latitude  = 18.496252;
private Double langitude = 73.802118;
private static View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.fragment_current_location, container, false);
    } catch (InflateException e) {  
        e.printStackTrace();
    }
    return view; 
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
     initialize();
    //displayMap();

}

 private void initialize() {

    // Search for the map fragment to finish setup by calling init().
   mapFragment = (MapFragment)   getActivity().getFragmentManager().findFragmentById(R.id.mapfragment);
   mapFragment.init(new OnEngineInitListener() {
        @Override
        public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
            if (error == OnEngineInitListener.Error.NONE) {
                map = null;
                map = mapFragment.getMap(); 
                // Set the map center to the Vancouver region (no animation)
                map.setCenter(new GeoCoordinate(latitude, langitude, 0.0),
                        Map.Animation.NONE); 
                map.getPositionIndicator().setVisible(true);
                try {
                    Image img_current_location = new Image();
                    img_current_location.setImageResource(R.drawable.marker);
                    map.getPositionIndicator().setMarker(img_current_location);

                } catch (IOException e) {
                    e.printStackTrace();
                } 
            } else {
                Log.e("HEREMAP", "Cannot initialize MapFragment (" + error + ")");
            }
        }
    });
}
@Override
public void onDestroyView() {
    super.onDestroyView();
    try{
        if (mapFragment != null)
            getActivity().getFragmentManager().beginTransaction().remove(mapFragment).commit();
    }catch (Exception e){
        e.printStackTrace();
    }

  }
}

当我们重新访问Map时,将其作为创建Map的InflateException如下所示。

 W/System.err: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f0a010b, tag null, or parent id 0xffffffff with another fragment for com.here.android.mpa.mapping.MapFragment

如何在Fragment中使用 HERE Map

提前谢谢!

1 个答案:

答案 0 :(得分:0)

马特(Matt)建议的答案是行之有效的,但它会导致重新创建和重绘地图,但这并不总是令人满意的。经过大量的反复试验,我找到了适合我的解决方案:

private static View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.map, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return view;
}

出于良好的考虑,这是带有R.id.mapFragment(android:id =“ @ + id / mapFragment”)的“ map.xml”(R.layout.map):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>